Struts2 + ajaxFileUpload 异步上传文件

阅读更多
ajaxFileUpload下载地址 http://www.phpletter.com/Our-Projects/AjaxFileUpload/

jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>




MyWebProject








	


action
public class uploadAction extends BaseAction {
private File file;
	private String fileFileName;
	private String fileFileContentType;
	private String message = "你已成功上传文件";

public void wxfileUpload() {
		HttpServletRequest request = this.getRequest();
		HttpServletResponse response = null;
		PrintWriter out = null;
		String encoding = "UTF-8";
		try {
			response = this.getResponse();
			response.setContentType("text/html;charset=utf-8");
			response.setCharacterEncoding(encoding);
			out = response.getWriter();
			request.setCharacterEncoding(encoding);
			String path =ServletActionContext.getServletContext().getRealPath(file.separator+"upload");
			File f = this.getFile();
			if(this.getFileFileName().endsWith(".exe")){
				message = "对不起,你上传的文件格式不允许!!!";
			}
			FileInputStream inputStream = new FileInputStream(f);
			File upfile=new File(path +File.separator+ this.getFileFileName());
			if(upfile.isDirectory() && !upfile.exists())
				upfile.mkdirs();
			else if(!upfile.isDirectory() && !upfile.getParentFile().exists())
				upfile.getParentFile().mkdirs();
			
			FileOutputStream outputStream = new FileOutputStream(upfile);
			byte[] buf = new byte[1024];
			int length = 0;
			while ((length = inputStream.read(buf)) != -1) {
				outputStream.write(buf, 0, length);
			}
			inputStream.close();
			outputStream.flush();
			outputStream.close();
			out.print("Success Upload");

		} catch (Exception e) {
			e.printStackTrace();
			out.print("Success ERROR");
			logger.info("WxAction reply exception:" + e);
		} finally {
			if (out != null)
				out.close();
		}
	}

}


//.....get  set()...


action配置文件


你可能感兴趣的:(struts,ajaxFileUpload)