利用ajax上传文件到服务器(服务端用的是Struts2)1.

1.html代码

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- 引入相关的js文件,相对路径  -->
<script src="jquery/jquery-1.10.2.js"></script>
<script type="text/javascript" src="jquery/ajaxfileupload.js"></script>
</head>
<script>
	jQuery(function() {
		$("#button").click(function() {
			//得到上传文件的名称
			var fileName = $("#fileToUpload").val().split("\\").pop();
			$.ajaxFileUpload({
				url : "api/uploadFile",//处理上传文件url
				secureuri : false,
				data : {
					name : encodeURI(fileName),//后台接收到的文件名乱码,用URL编码防止乱码,后台用URLDecoder解码
					description : "这是文件描述" //这个也应该用url编码
				},
				fileElementId : "fileToUpload",//file控件id
				dataType : "json",
				success : function(data) {
					alert(data);
				},
				error : function(data) {
					alert(data);
				}
			});
			return false;
		});
	});
</script>
<body>
	<input id="fileToUpload" type="file" name="fileToUpload" class="input">
	<button id="button">上传</button>
</body>
</html>

2.服务端代码(Action代码)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class ApiAction extends ActionSupport implements RequestAware{
	private static final long serialVersionUID = -5679214991812521127L;
	
	private Map<String, Object> request;
	private File fileToUpload;
	private String name;
	private String description;
	public Map<String, Object> responseJson = new HashMap<String, Object>();

	public String handleFile(){
		try {
			//解决传递过来的文件名乱码问题
			String fileName = URLDecoder.decode(name, "UTF-8");  
			FileInputStream inputStream = new FileInputStream(fileToUpload);
	        FileOutputStream outputStream = new FileOutputStream("D:/data/" + fileName);
	        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();
	        System.out.println("保存成功。name="+fileName+",description="+description);
	        responseJson.put("msg", "保存成功。name="+fileName+",description="+description);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	public File getFileToUpload() {
		return fileToUpload;
	}
	public void setFileToUpload(File fileToUpload) {
		this.fileToUpload = fileToUpload;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}

	public Map<String, Object> getResponseJson() {
		return responseJson;
	}

	public void setResponseJson(Map<String, Object> responseJson) {
		this.responseJson = responseJson;
	}
}

3.Struts2配置

返回的是json格式。

 <package name="api" namespace="/api" extends="json-default">
        
        <action name="uploadFile" class="com.alisa.action.ApiAction" method="handleFile">
        	<result name="success" type="json">
        		<param name="root">responseJson</param>
        	</result>
        </action>
    </package>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值