LayUIコンポーネントjavaベースのファイルアップロード
LayUIコンポーネントjavaベースのファイルアップロード Mavenガイド jspページ jsページ 構成SpringMVCアップロード javaバックグラウンド
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
dependency>
layui.use('upload', function(){
var upload = layui.upload;
// Excel
var uploadInst=upload.render({
elem: '#uploadfile', //
url: projectName+'/hardware/uploadfile.do', //
method:'POST',
accept: 'file', //
size: 204800, // , KB
done: function(res){
//
console.log(res)
layer.msg(" ",{
icon: 1});
parent.location.reload();
},
error: function(){
//
layer.msg(" ",{
icon: 0});
}
});
})
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSizePerFile" value="5242880"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="resolveLazily" value="true"/>
bean>
/**
* Excel
* @throws IOException
* @throws IllegalStateException
*/
@RequestMapping("/uploadfile.do")
@ResponseBody
public Map<String,String> uploadfile(@RequestParam("file") MultipartFile file,HttpServletRequest request)
throws IllegalStateException, IOException{
if(null!=file){
String fileOrigName=file.getOriginalFilename();//
if (!fileOrigName.contains(".")) {
throw new IllegalArgumentException(" ");
}
//
fileOrigName = fileOrigName.substring(fileOrigName.lastIndexOf("."));
String newfileName =CommonFunction.strAutoInfo() +fileOrigName;
// D:\apache-tomcat-8.0.53\webapps\QrCode\
String uploadPath=request.getServletContext().getRealPath("");
// QrCode
String projectName=request.getServletContext().getContextPath().substring(1);
// D:\apache-tomcat-8.0.53\webapps/uploadfile\
String newdestPath= uploadPath.replaceAll(projectName, "uploadfile")+newfileName;
//
File newfile=new File(newdestPath);
if(!newfile.exists() && !newfile.isDirectory()){
System.out.println(newfile+" , ");
//
newfile.mkdirs();
}
file.transferTo(newfile);
}
Map<String,String> map=new HashMap<String,String>();
map.put("code", "0");
map.put("msg"," ");
return map;
}