Jersey Backend service receives image uploads from Ajax frontend

Source: Internet
Author: User

Recent projects need to receive processing at the front end of the upload picture backend. The front-end JSP page uses Ajax to upload the picture back end using the Jersey framework to provide a restful interface to receive processing pictures.

First, the front-end processing

A picture upload in a JSP page does not use the form form but directly uses the file type input control

<input type= "File" name= "file" class= "Inptxta" value= "" id= "Applogo"/><input class= "inptxta" value= "" id= " AppID "/>

Use AJAX to process file uploads in JS

var formData = new FormData ();//use FormData to upload data//content-type is form-data type Formdata.append ("AP PId ", $ (" #appID "). Val ()); Formdata.append (" File ", $ (" #appLogo ") [0].files[0]);//take file of type input $.ajax ({method:" POST ", url:"/apps/updateapp ", timeout:10000,//time-out setting unit milliseconds crossdomain:true,async:false,headers: {" Client-type ":" Pla Tform "},datatype:" JSON ", data:formdata,contenttype:false,//processdata:false,//data does not do preprocessing success:function (response {alert (response.msg); Return;},error:function (e) {alert (response.msg); return;}});


Second, service-side processing

The

Server uses the Jersey framework to provide a restful interface because the data is passed as a form-data parameter type, so the parameter type in the service-side interface is specified as @formdataparam.

/** Application Information Update * File Upload * File description information in InputStream type to formdatacontentdisposition object encapsulation. */@Path ("Updateapp") @POST @consumes (mediatype.multipart_form_data) Public response updateapp (@ Formdataparam ("file")  InputStream inputStream             , @FormDataParam ("file")  FormDataContentDisposition cp              , @FormDataParam ("AppId") string appid) {     string name = cp.getfilename ();    try{         name = new string (Name.getbytes ("iso-8859-1"), "UTF-8");     }catch (exception e) {        result.put ("Errcode",  1)     result.put ("msg",  "app icon name exception");    return  Jsonutil.tojsonresponse (result);     }    ... Further processing}


Iii. possible anomalies

1, in the development process of the front-end of the Formdata package may appear problems prompt invocation TypeError and other exceptions. The general reason is that these two parameters are not added

contenttype:false,//processdata:false,//data is not preprocessed

These two parameters affirm that data preprocessing is not done. If missing then the front end is preprocessed when encapsulating the data such as x-www-form-urlencoded will encapsulate the parameters into the URL. Encapsulation exceptions may occur for data preprocessing of formdata types.


2. If the parameter type is not specified correctly when the backend service receives the parameter, the following exception may be reported

Critical: A message body Reader for Java class javax.servlet.http.HttpServletRequest, and Java type interface javax.servlet.http . HttpServletRequest, and MIME media type multipart/form-data;boundary=----WEBKITFORMBOUNDARYRJ7E8B7MDZDGJBHG is not Found. The registered message body readers compatible with the MIME media type are:multipart/*-com.sun.jersey.multipart.im Pl.  multipartreaderserverside*/*-Com.sun.jersey.core.impl.provider.entity.FormProvider  Com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider Com.sun.jersey.core.impl.provider.entity.StringProvider

This is because the parameter is uploaded in formdata format if the interface does not specify a parameter type of @formdataparam, the above exception prompt appears.


Jersey Backend service receives image uploads from Ajax frontend

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.