Front Code:
Bindphoto (e) {var that = This;wx.chooseimage ({count:1,sizetype: [' original ', ' compressed '],//specifies the original or compressed diagram sourcetype: [' Album ', ' Camera '],//Specify picture source Success:function (res) {var tempfilepaths = res.tempfilepaths;wx.uploadfile ({url: '/http. 192.168.31.111:8007/goods/wx_upload.do ', filepath:tempfilepaths[0],name: ' File ', header: {"Content-type": " Multipart/form-data "},formdata:{' Session_token ': token},success:function (res) {var cur_data = Res.data;console.log ( Cur_data.filename); },fail:function (res) {Console.log (' upload failed ') (}})})}, background code
/*Small program upload picture test*/@RequestMapping ("wx_upload.do") public void Wx_upload (HttpServletRequest request, httpservletresponse response) Throws Exception {request.setcharacterencoding ("utf-8");//Set Encoding//Get Disk File entry factoryDiskfileitemfactory factory = new Diskfileitemfactory (); Servletfileupload upload = new Servletfileupload (factory); String fileId = null; String json = "{\" success\ ": False,\" filename\ ": \" "+ fileId +" \ "}"; String Pathurl = fsdefaultmgr.E_default. Getdefaultuploadpathurl ();//Get Picture Server pathInputStream instream = null; try {//multiple files can be uploadedlist<fileitem> list = (list<fileitem>) upload.parserequest (request); for (Fileitem item:list) {//get the property name of the formString name = Item.getfieldname ();//if the obtained form information is plain text informationif (Item.isformfield ()) {//gets the user-specific input string, the name is very good, because the form submitted by the string type ofString value = item.getstring (); Request.setattribute (name, value); }else {//Get path nameString filename = Item.getname (); Request.setattribute (name, filename); Instream = Item.getinputstream (); }} bytearrayoutputstream Swapstream = new Bytearrayoutputstream (); byte[] buff = new byte[100]; int rc = 0; while (rc = instream.read (buff, 0,)) > 0) {swapstream.write (buff, 0, RC); } byte[] bytes = Swapstream.tobytearray (); fileId = fastdfsclient.uploadfile (Bytes, "20161545454.png", null); if (fileId! = null) {json = "{\" success\ ": True,\" pathurl\ ": \" "+ Pathurl +" \ ", \" filename\ ": \" "+ fileId +" \ " }"; } response.getwriter (). write (JSON); Response.getwriter (). Flush (); Response.getwriter (). Close (); }catch (Exception e) {e.printstacktrace (); }}
When using the SPRINGMVC framework:SPRINGMVC Servletfileupload.parserequest (Request) resolution to NULL get no data problem cause analysis
First, let's look at the configuration of file uploads in spring MVC.
- <bean id= "Multipartresolver"
- class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
- <property name= "defaultencoding" value= "UTF-8"/>
- <property name= "maxuploadsize" value= "2000000000"/>
- </bean>
Let's take a look at the controller use
- Public void Upload2 (HttpServletRequest request) {
- //transition to Multiparthttprequest
- try {
- Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
- list<multipartfile> fileList = multipartrequest.getfiles ("file");
- For (Multipartfile mf:filelist) {
- if (!mf.isempty ()) {
- }
- }
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
Way Two
- Public String upload (httpservletrequest request,
- @RequestParam (value = "file") multipartfile[] files) {
- try {
- For (Multipartfile mf:files) {
- if (!mf.isempty ()) {
- }
- }
- } catch (Exception e) {
- E.printstacktrace ();
- }
- return "upload";
- }
- Here Springmvc all for us to encapsulate into their own file object, the conversion process is in our configuration of Commonsmultipartresolver this converter inside the next look at its source
His converter inside is called Common-fileupload parsing, and then use the Parsefileitems () method to encapsulate their own file objects.
List<fileitem> Fileitems = ((servletfileupload) fileUpload). Parserequest (Request);
We should have found the above code, has used FileUpload resolved request, you in controller inside received request is already resolved, you use upload to parse to get is definitely empty, this is the problem ( You can experiment in the servlet to see if you can get the data after the second parse, of course not.
Solution Solutions
1) Remove the spring MVC file upload configuration
- <!--
- <bean id= "Multipartresolver"
- class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
- <property name= "defaultencoding" value= "UTF-8"/>
- <property name= "maxuploadsize" value= "2000000000"/>
- </bean>
- -
Within the controller to complete the request of the resolution (of course, the above spring MVC provides two methods are not available, all the uploaded places need to do their own processing)
- Public void Upload3 (HttpServletRequest request) {
- Diskfileitemfactory factory = new Diskfileitemfactory ();
- Servletfileupload upload = new Servletfileupload (Factory);
- try {
- list<fileitem> list = upload.parserequest (request);
- For (Fileitem item:list) {
- if (Item.isformfield ()) {
- }else{
- //item.write (New File (""));
- }
- }
- } catch (Fileuploadexception e) {
- E.printstacktrace ();
- }
- }
2) If it is necessary to use the Progresslistener listener we can rewrite the Commonsmultipartresolver parserequest method
- Package com.lwp.spring.ext;
- Import java.util.List;
- Import Javax.servlet.http.HttpServletRequest;
- Import Org.apache.commons.fileupload.FileItem;
- Import Org.apache.commons.fileupload.FileUpload;
- Import Org.apache.commons.fileupload.FileUploadBase;
- Import org.apache.commons.fileupload.FileUploadException;
- Import Org.apache.commons.fileupload.servlet.ServletFileUpload;
- Import org.springframework.web.multipart.MaxUploadSizeExceededException;
- Import org.springframework.web.multipart.MultipartException;
- Import Org.springframework.web.multipart.commons.CommonsMultipartResolver;
- Import Com.lwp.listener.FileUploadListener;
- Public class Commonsmultipartresolverext extends Commonsmultipartresolver {
- @Override
- protected Multipartparsingresult parserequest (HttpServletRequest request)
- throws Multipartexception {
- Fileuploadlistener listener = new Fileuploadlistener ();
- String encoding = determineencoding (request);
- FileUpload FileUpload = preparefileupload (encoding);
- Fileupload.setprogresslistener (listener);
- try {
- List<fileitem> Fileitems = ((servletfileupload) fileUpload). Parserequest (Request);
- return Parsefileitems (Fileitems, encoding);
- }
- catch (Fileuploadbase.sizelimitexceededexception ex) {
- throw New Maxuploadsizeexceededexception (Fileupload.getsizemax (), ex);
- }
- catch (Fileuploadexception ex) {
- throw New Multipartexception ("Could not parse multipart servlet request", ex);
- }
- }
- }
Listener method
- Import Org.apache.commons.fileupload.ProgressListener;
- Public class Fileuploadlistener implements Progresslistener {
- @Override
- public void Update (long arg0, long arg1, int arg2) {
- //arg0 How many bytes have been uploaded
- //arg1 altogether how many bytes
- //arg2 is uploading the first few files
- System.out.println (arg0 +"\ T" + arg1 +"\ T" + arg2);
- }
- }
Config file changed to our own (the flaw in this way is that all file uploads need to be used to listener)
- <bean id= "Multipartresolver"
- class= "Com.lwp.spring.ext.CommonsMultipartResolverExt" >
- <property name= "defaultencoding" value= "UTF-8"/>
- <property name= "maxuploadsize" value= "2000000000"/>
- </bean>
Note: In summary, if only normal file upload spring MVC can complete, if you need to use the progress bar of the listener front can use the false progress bar or the above two ways.
Small program File Upload UploadFile