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

前端之家收集整理的这篇文章主要介绍了利用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,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>
原文链接:https://www.f2er.com/ajax/162190.html

猜你在找的Ajax相关文章