Home > Back-end >  After receive WeChat small programs with pictures, comprised the struts. Only larger than 2 m file c
After receive WeChat small programs with pictures, comprised the struts. Only larger than 2 m file c

Time:09-21

Front end little program code:
The function send_photo (data, successfun) {
Var that=this
The console. The log (" data length="+ data. Length)
The console. The log (" data path="+ data [0] +" ")
Wx. UploadFile ({
Url: '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * upload in action',//for the sample only, not real interface address you write their own mapping Java interface path
The content-type header: {" ":" multipart/form - the data "},
FilePath: data [0] + ", "
Name: 'file',
FormData: {
Method: 'POST'
},
Success: the function (res) {
Successfun (res)
}
})
}
Background receive Java code:
Public void upload () throws the Exception {
Try {
The Date the Date=new Date ();
//upload file
HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
DiskFileItemFactory factory=new DiskFileItemFactory();
Factory. SetSizeThreshold (4096);//set the buffer size, here is the 4 KB
String=basePath ServletActionContext. GetRequest (). GetRealPath ("/");//get the root directory
//set the download directory
String uploadPath=basePath + "/upload";
//set the temporary directory
String tempPath=uploadPath + "/temp";
//uploadPath=uploadPath + "/" + date. The getTime ();//add time to prevent repeated
Enclosing initDir (uploadPath tempPath);
The File tempPathFile=new File (tempPath);
Factory. SetRepository (tempPathFile);//set the buffer
ServletFileUpload upload=new ServletFileUpload(factory);
//upload. SetSizeMax (34194304); Set the maximum file size, 4 MB
List the items=upload. ParseRequest (request);//get all the files
The Iterator itr=items. The Iterator ();
String imgUrl;

While (itr. HasNext ()) {
FileItem FileItem=(FileItem) itr. Next ();
If (fileItem isFormField ()) {
System. The out. Println (" is the form parameter name: "+ fileItem. GetFieldName () +", the form parameter values: "+ fileItem. Get string (" utf-8"));
} else {
If (fileItem. GetName ()!=null & amp; & !" ". The equals (fileItem getName ())) {
System. The out. Println (" upload file size: "+ fileItem. GetSize ());
System. The out. Println (" upload a file type: "+ fileItem. GetContentType ());
System. The out. Println (" upload file name: "+ fileItem. GetName ());
//save the file
File saveFile=new File (uploadPath, fileItem getName ());
FileItem. Write (saveFile);
ImgUrl=uploadPath + "/" + fileItem getName ();
System. The out. Println (" upload success ");
} else {
System. The out. Println (" no upload a file ");
}
}

}

Writer out=response. GetWriter ();
Out. Write (" success ");
out.flush();
} the catch (Exception e) {
e.printStackTrace();
Throw new Exception (e);
}
}

Larger than 2 MB file successfully received, the configuration is correct, the code should be correct, this example tomcat 8.0, jdk1.8,

CodePudding user response:

Is less than 2 m is not successful?

CodePudding user response:

Why not use spring boot? Can be set on the gateway is the size of the file upload, including a single file size and total size can be set up, do not need some into the code, convenient maintenance,

CodePudding user response:

Can also use nginx forwarding, restricting the size of the file, wrote the code inside the late maintenance updates very troublesome,
  • Related