RESTful JAX-RS文件上传示例

就像上一页中的下载一样, 我们可以轻松上传文件, 例如图像文件, pdf文件, excel文件, 文本文件等。
@FormDataParam(“ file”)批注用于在服务类中提及文件参数。 @Consumes(MediaType.MULTIPART_FORM_DATA)用于提供文件上传的信息。
要使用JAX-RS API上传文件, 我们正在使用jersey实现。
单击我下载jersey jar文件。
要通过jersey实施上传文件, 你需要在web.xml文件中提供额外的配置条目。

< init-param> < param-name> jersey.config.server.provider.classnames< /param-name> < param-value> org.glassfish.jersey.filter.LoggingFilter; org.glassfish.jersey.media.multipart.MultiPartFeature< /param-value> < /init-param>

让我们看一下使用RESTful JAX-RS API上传文件的完整代码。
JAX-RS文件上传档案:FileUploadService.java
package com.srcmini.rest; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import org.glassfish.jersey.media.multipart.FormDataParam; @Path("/files") public class FileUploadService { @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile( @FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) { String fileLocation = "e://" + fileDetail.getFileName(); //saving file try { FileOutputStream out = new FileOutputStream(new File(fileLocation)); int read = 0; byte[] bytes = new byte[1024]; out = new FileOutputStream(new File(fileLocation)); while ((read = uploadedInputStream.read(bytes)) != -1) { out.write(bytes, 0, read); } out.flush(); out.close(); } catch (IOException e) {e.printStackTrace(); } String output = "File successfully uploaded to : " + fileLocation; return Response.status(200).entity(output).build(); } }

文件:web.xml
< ?xml version="1.0" encoding="UTF-8"?> < web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> < servlet> < servlet-name> Jersey REST Service< /servlet-name> < servlet-class> org.glassfish.jersey.servlet.ServletContainer< /servlet-class> < init-param> < param-name> jersey.config.server.provider.packages< /param-name> < param-value> com.srcmini.rest< /param-value> < /init-param> < init-param> < param-name> jersey.config.server.provider.classnames< /param-name> < param-value> org.glassfish.jersey.filter.LoggingFilter; org.glassfish.jersey.media.multipart.MultiPartFeature< /param-value> < /init-param> < load-on-startup> 1< /load-on-startup> < /servlet> < servlet-mapping> < servlet-name> Jersey REST Service< /servlet-name> < url-pattern> /rest/*< /url-pattern> < /servlet-mapping> < /web-app>

档案:index.html
< h2> File Upload Example< /h2> < form action="rest/files/upload" method="post" enctype="multipart/form-data"> < p> Select a file : < input type="file" name="file" size="45" /> < /p> < input type="submit" value="http://www.srcmini.com/Upload File" /> < /form>

现在, 在服务器上运行此应用程序, 你将看到以下输出:
输出:
File successfully uploaded to e://myimage.png

【RESTful JAX-RS文件上传示例】单击我下载此示例

    推荐阅读