1.需要导入jar包 commons-fileupload.jar commons-io.jar
2.springmvc需要添加的配置文件
<!-- 文件上传相关 id="multipartResolver"必须是multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<!-- 设定文件上传的最大值50MB,50*1024*1024 -->
<property name="maxUploadSize" value="52428800"></property>
</bean>
3.form表单代码
<form id="uploadForm" action="${pageContext.request.contextPath}/admin/tdupload/upload" method="post" enctype="multipart/form-data">
<input id="filePath" type="hidden" />
<input name="gsmc" value="天津交单" type="hidden" />
<input name="tdh" value="${requestScope.tdh}" type="hidden" />
<table class="table">
<tr>
<th><fmt:message key="download.fileType"/></th>
<td><select id="tjfileType" name="fType">
<option value="提单">提单</option>
<option value="随车单据">随车单据</option>
</select>
</td>
</tr>
<tr>
<th><fmt:message key="file.upload.btn"/></th>
<td>
<input name="file" type="file" multiple="multiple">
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center;">
<button type="button" class="easyui-linkbutton" onclick="uploadFun()">保存</button>
<!-- <a id="saveBtn" href="#" class="easyui-linkbutton" type="submit" onclick="saveTj()">保存</a> -->
<a id="offBth" href="#" class="easyui-linkbutton" onclick="cancel()">取消</a>
</td>
</tr>
</table>
</form>
4.js中ajax异步请求代码
function uploadFun(){
var formData = new FormData($("#uploadForm")[0]);
$.ajax({
url : "${pageContext.request.contextPath}/admin/tdupload/upload",
type : 'POST',
data : formData,
processData : false,
contentType : false,
async : false,
success : function(data) {
if(data == "success"){
$("#tjdl").dialog("close");
refresh();
}else{
$("#tjdl").dialog("close");
$.messager.alert(data);
}
},
error : function(x){
$("#tjdl").dialog("close");
$.messager.alert("上传文件异常");
}
});
}
5.java控制器代码
@RequestMapping(value="/upload", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
@ResponseBody
public String fileUploadCon(@SessionModel Staff staff,
@RequestParam(value = "file") MultipartFile[] files,HttpServletRequest request,
HttpServletResponse response,String tdh,String gsmc,String fType) throws Exception{
String rootPath = this.properties.getProperty("server.dist");// 跟路径 E:\\
String rootURI = this.properties.getProperty("server.uri");// 相对路径 /server
String path =rootPath+ rootURI + "/"+tdh;
try {
for (MultipartFile file : files) {
String fileName = file.getOriginalFilename();
File filePath = new File(path, fileName);
// 如果文件目录不存在,创建目录
if (!filePath.getParentFile().exists()) {
filePath.getParentFile().mkdirs();
System.out.println("创建目录" + filePath);
}
// 写入文件
file.transferTo(filePath);
//保存上传记录
TdFile tdFile=new TdFile();
tdFile.setTdh(tdh);
tdFile.setFileName(fileName);
tdFile.setFilePath(filePath.getAbsolutePath());
tdFile.setScr(staff.getName());
String serverUrl=filePath.getAbsolutePath().substring(2).replaceAll("\\\\", "/");
tdFile.setServerUrl(serverUrl);
tdFile.setStatus(0);
tdFile.setFileOrderId("ZSY"+new Date().getTime());
tdFile.setFileType(fType);
tdFile.setGsmc(gsmc);
tdFileService.insertTdFile(tdFile);
}
} catch (Exception e) {
return e.getMessage();
}
return "success";
}