jersey+jetty实现文件上传
Posted shenyuchong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jersey+jetty实现文件上传相关的知识,希望对你有一定的参考价值。
服务配置与启动类
import org.glassfish.jersey.servlet.ServletContainer; import javax.ws.rs.Path; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlets.CrossOriginFilter; /** * 功能:服务启动 * @author yuchong.shen * 2019年1月10日 * */ @Path("/") public class RestInterface { private void start(int port) throws Exception { //设置扫描包及配置multipart ServletHolder servletHolder = new ServletHolder(ServletContainer.class); servletHolder.setInitParameter("jersey.config.server.provider.packages","yuchongshen.uploader"); servletHolder.setInitParameter("jersey.config.server.provider.classnames","org.glassfish.jersey.media.multipart.MultiPartFeature"); //servlet上下文 ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); servletContextHandler.addServlet(servletHolder, "/*"); //解决前端跨域访问问题 FilterHolder filterHolder = new FilterHolder(CrossOriginFilter.class); filterHolder.setInitParameter("allowedOrigins", "*"); filterHolder.setInitParameter("allowedMethods", "GET, POST"); servletContextHandler.addFilter(filterHolder, "/*", null); //创建服务 Server server = new Server(Config.PORT); server.setHandler(servletContextHandler); server.start(); System.out.println("******************************************************************"); System.out.println("server has startup,port:"+Config.PORT); System.out.println("server has startup,upload path:"+Config.UPLOAD_PATH); System.out.println("******************************************************************"); } public static void main(String[] args) throws Exception { int portIndex=0,uploadPathIndex=0; for(int i=0;i<args.length;i++){ if(args[i].equals("-uploadpath")) uploadPathIndex=i+1;//获取上传路径根目录 if(args[i].equals("-port")) portIndex=i+1;//获取端口号 } if(uploadPathIndex>0) Config.UPLOAD_PATH = args[uploadPathIndex]; if(portIndex>0) Config.PORT = Integer.parseInt(args[portIndex]); RestInterface server = new RestInterface(); server.start(Config.PORT); }
编写上传服务类
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.inject.Singleton; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import org.glassfish.jersey.media.multipart.FormDataParam; /** * 功能:处理上传 * @author yuchong.shen * 2019年1月10日 * */ @Path("/upload") @Singleton public class Uploader { @POST @Consumes({MediaType.MULTIPART_FORM_DATA}) public String uploadFile(@FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition fileMetaData, @FormDataParam("path") String path, @FormDataParam("file_name") String fileName) throws Exception { String postfix = fileMetaData.getFileName().substring(fileMetaData.getFileName().lastIndexOf(".")); if(fileName==null){ fileName = new String(fileMetaData.getFileName().getBytes("iso8859-1"),"utf-8"); }else{ fileName = fileName+postfix; } if(path==null) path = ""; try { int read = 0; byte[] bytes = new byte[1024]; OutputStream out = new FileOutputStream(new File(Config.UPLOAD_PATH+File.separatorChar +path+ File.separatorChar+fileName)); while ((read = fileInputStream.read(bytes)) != -1) { out.write(bytes, 0, read); } out.flush(); out.close(); } catch (IOException e) { throw new WebApplicationException(); } return "uploaded successfully"; } }
相关依赖
<properties> <jersey2.version>2.19</jersey2.version> <jaxrs.version>2.0.1</jaxrs.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.4.14.v20181114</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlets</artifactId> <version>9.4.14.v20181114</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>9.4.14.v20181114</version> </dependency> <!-- JAX-RS --> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>${jaxrs.version}</version> </dependency> <!-- Jersey 2.19 --> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>${jersey2.version}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>${jersey2.version}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>${jersey2.version}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> <version>2.19</version> </dependency> </dependencies>
启动
maven打包执行
mvn package
进入target目录执行:
java -jar uploader-0.0.1-SNAPSHOT.jar
服务启动完毕,默认端口为8888,上传根目录为jar包所在目录
配置端口或上传根目录
启动时带参数,例如:
java -jar target/uploader-0.0.1-SNAPSHOT.jar -port 8777 -uploadpath C:Usersyuchongshen
访问
http://ip:端口/upload
restful api
前端使用post方法mulitpart/form_data
参数名 | 类型 | 描述 |
file | File | 上传的文件 |
path | String | 上传在根目录下的哪个目录,必须事先创建 |
file_name | String | 上传文件改名 |
以上是关于jersey+jetty实现文件上传的主要内容,如果未能解决你的问题,请参考以下文章
带有 XML 参数的 REST 服务操作上的 HTTP 错误 415 不受支持的媒体类型(Jersey + Jetty)