Struts2 + ajaxFileUpload 异步上传文件

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

jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!DOCTYPE html>
<head>
<title>MyWebProject</title>
<meta charset="utf-8" />
<link type="text/css" href="${pageContext.request.contextPath}/jsnew/ajaxfileupload.css" rel="stylesheet" />
<script type="text/javascript" src="${pageContext.request.contextPath}/jsnew/jquery.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/jsnew/ajaxfileupload.js"></script>
<script type="text/javascript">
function ajaxFileUpload() {
	$.ajaxFileUpload({
		url : "${pageContext.request.contextPath}/space/quicklogin/fileUpload.html",   //submit to UploadFileServlet
		secureuri : false,
		fileElementId : "file", //input file 的id
		dataType : "text", //or json xml whatever you like~
		success : function(data, status) {
			$("#result").append(data);
		},
		error : function(data, status, e) {
			$("#result").append(data);
		}
	});
	return false;
}
</script>
</head>

<body>
	<input id="file" type="file" size="45" name="file" class="input"/><br/>
	<button class="button" id="buttonUpload" οnclick="return ajaxFileUpload();">Upload</button>
	<div id="result"></div>
</body>
</html>

</html>


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配置文件
<action name="fileUpload"   class="com.bjdata.bjsearch.presentation.action.weix.uploadAction" method="fileUpload">
</action>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值