"Jersey" image upload and display

Source: Internet
Author: User

first, pre-preparation

Some of the dependencies you need to upload images:

<dependency>      <groupId>org.jvnet.mimepull</groupId>      <artifactid>mimepull</ artifactid>      <version>1.7</version></dependency><dependency>       < Groupid>com.sun.jersey.contribs</groupid>       <artifactId>jersey-multipart</artifactId>       <version>${jersey.version}</version></dependency>
Second, Jersey annotation

@Path ("/img")

Indicates that the access path is/IMG and can receive parameters such as @path ("/images/{name}.{ Type} "), and then use @pathparam to receive the name and type two parameters, and also support regular expressions such as @path (" username/{username:[a-za-z][0-9]*} ")

@POST

Represents an accepted HTTP request type of post

@Consumes (Mediatype.multipart_form_data)

Indicates that the accepted data type is "Multipart/form-data"

@Produces (Mediatype.application_json)

Indicates that the data type that occurred is "Application/json"

@FormDataParam

Receive specific types of data such as pictures, files, etc.

@Context

Receive the parameters sent in the POST request

@QueryParam

To get the parameters in the URL in the GET request, such as the request URL is Http://localhost:8080/user?username=Amy&age=12&gender=male, you can use:

@Path ("/user") @GET the public AddUser (@QueryParam ("username") String username,               @QueryParam (  int age ,               @QueryParam ("Gender") String gender) {    //  .....}                        

To receive these three parameters.

@PathParam

Get URL Path parameter

Third, the Code
1  Public Static FinalString Imgpath = "d:/imgaes/";2@Path ("/uploadimg")3 @POST4 @Produces (Mediatype.application_json)5 @Consumes (mediatype.multipart_form_data)6      PublicString uploadimg (@FormDataParam ("File") InputStream FileInputStream,7@FormDataParam ("File") formdatacontentdisposition datacontentdisposition,8 @Context httpservletrequest request) {9String imgname = Calendar.getinstance (). Gettimeinmillis () +datacontentdisposition.getfilename ();TenFile File =NewFile (Imgpath +imgname); One         Try { A fileutils.copyinputstreamtofile (fileinputstream, file); -}Catch(IOException e) { -  the e.printstacktrace (); -         } -Jsonbuilder Resultjson =NewJsonbuilder (); -Resultjson.append ("ret", Request.getscheme () + "://" + request.getservername () + ":" ++ request.getserverport () + "/app/images/" +imgname); -         returnresultjson.tostring (); +     } A  at@Path ("/images/{name}.{ Type} ") - @GET -      Public voidShowimg (@PathParam ("name") String ImageName, -@PathParam ("type") String type, - @Context httpservletresponse Response) -             throwsIOException { inInputStream InputStream =NULL; -OutputStream out =NULL; to         Try { +File File =NewFile (Imgpath + imageName + "." +type); -InputStream =Newfileinputstream (file); theout =Response.getoutputstream (); *             //pic size = 1M $             byte[] bytes =New byte[1024 * 1024];Panax Notoginseng             intLen = 0; -              while(len = inputstream.read (bytes)) > 0) { theOut.write (Bytes, 0, Len); +             } A}Catch(Exception e) { the e.printstacktrace (); +}finally { -             if(InputStream! =NULL) $ inputstream.close (); $             if(Out! =NULL) - out.close (); -         } the}

"Jersey" image upload and display

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.