HTML
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>附件上傳窗口</title>
<jsp:include page="/WEB-INF/views/taglib.jsp"></jsp:include> <!--在这里引入jquery-->
<script type="text/javascript" src="${ctx}resources/js/windows/win_test_attach.js"></script>
<script language="javascript" type="text/javascript" src="${ctx}resources/js/ajaxfileupload.js"></script>
<script type="text/javascript">
$(document).ready(function(){
initPage();
});
</script>
</head>
<body>
<div id="toolbar">
<button id="btnLeave" style="font-size:10pt;" >離開</button>
</div>
<br />
<div>
<span class="spanQuery">檔案:</span>
<input id="fileId" name="fileId" type="file" />
<input id="btnUpload" type="button" value="上傳">
</div>
<br />
<table id="tblAttach"></table>
<input type="hidden" id="hdnLoginUser" value="${loginUserName }">
</body>
</html>
JS
var fileId = "";
var fileName = "";
function ajaxUpload() {
fileId = $("#fileId").val();
if (fileId != null && fileId != '') {
$.ajaxFileUpload( {
url : 'win_upload_file',
secureuri : false,
fileElementId : 'fileId',
dataType : 'json',
success : function(data, status) {
fileName = data.result;
addRowToGrid();
},
error : function(data, status, e) {
alert(" upload error: " + data + " Status: " + status + " e: " + e);
}
});
} else {
alert("請選擇檔案!");
}
}
@RequestMapping(value = "/win_upload_file", method = RequestMethod.POST)
@ResponseBody
public String winSaveAttach(HttpServletRequest request, HttpServletResponse response) {
String userPath = ConfigurationManager.getConfig("ATTACHMENT_PATH"); //这里只是设置文件存储路径
System.out.println("userUploadPath: " + userPath);
File attachPath = new File(userPath);
if(!attachPath.exists()) {
attachPath.mkdir();
}
UploadHelper helper = new UploadHelper(); //这里文件上传处理辅助类
helper.setTmpPath(userPath);
helper.setSavePath(userPath);
List<String> resultList = new ArrayList<String>();
String result = "";
Map<String,String> map = new HashMap<String,String>();
try {
resultList = helper.handleUplodFile(request);
map.put("result", resultList.get(0));
result = "{\""+ "result" + "\":\"" + resultList.get(0) + "\"}";
System.out.println("result: " + result);
} catch (FileUploadException e) {
map.put("result", e.getMessage());
e.printStackTrace();
} catch (IOException e) {
map.put("result", e.getMessage());
e.printStackTrace();
}
return result;
}
文件上传处理辅助类
public class UploadHelper {
private String tmpPath;
private String savePath;
/**
* @return the tmpPath
*/
public String getTmpPath() {
return tmpPath;
}
/**
* @param tmpPath the tmpPath to set
*/
public void setTmpPath(String tmpPath) {
this.tmpPath = tmpPath;
}
/**
* @return the savePath
*/
public String getSavePath() {
return savePath;
}
/**
* @param savePath the savePath to set
*/
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public List<String> handleUplodFile(HttpServletRequest request) throws FileUploadException, IOException{
File tmpDir = new File(getTmpPath());
File saveDir = new File(getSavePath());
List<String> result = new ArrayList<String>();
if (!ServletFileUpload.isMultipartContent(request)) {
return result;
}
DiskFileItemFactory dff = new DiskFileItemFactory();
dff.setRepository(tmpDir);// 指定上传文件的临时目录
dff.setSizeThreshold(1024000);// 指定在内存中缓存数据大小,单位为byte
ServletFileUpload sfu = new ServletFileUpload(dff);// 创建该对象
FileItemIterator fii;
fii = sfu.getItemIterator(request);
while (fii.hasNext()) {
FileItemStream fis = fii.next();
if (fis.isFormField() || fis.getName().length() == 0)
continue;
String fileName = formatFileName(fis.getName());
BufferedInputStream in = new BufferedInputStream(fis.openStream());// 获得文件输入流
File file = new File(saveDir, fileName);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));// 获得文件输出流
Streams.copy(in, out, true);// 开始把文件写到你指定的上传文件夹
result.add(fileName);
}
return result;
}
private String formatFileName(String fileName){
return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + fileName.substring(fileName.lastIndexOf("."));
//return fileName;
}
}