关键代码:
application.java中
register(MultiPartFeature.class);
前端html中:
<form action="${path}/list/uploadExcel" method="post" enctype="multipart/form-data" accept-charset="utf-8"> <p> <input type="file" name="file" /><br /> </p> <input type="submit" value="上传" /> </form>
服务端(有略微的名称编码问题):
<span> </span>@POST @Path("uploadExcel") @Consumes(MediaType.MULTIPART_FORM_DATA) public String uploadExcel( @FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition disposition) { String imageName = Calendar.getInstance().getTimeInMillis() + disposition.getFileName(); //File file = new File("D:\\" + imageName); return "import data succeed"; }
by 刘迎光@萤火虫工作室
OpenBI交流群:495266201
MicroService 微服务交流群:217722918
mail: liuyg#liuyingguang.cn
博主首页(防止爬虫):http://blog.liuyingguang.cn
OpenBI问答社区:http://openbi.liuyingguang.cn/
<servlet> <servlet-name>Rest_Servlet</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>cn.firewarm.testRestShiro.rest</param-value> </init-param> <init-param> <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> <param-value>cn.firewarm.testRestShiro.filter.TestFilter</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> |
public class TestFilter implements ContainerRequestFilter { public ContainerRequest filter(ContainerRequest request) { if(判断条件){ return request; }else{ Response response = Response.ok("error").status(401).type(MediaType.APPLICATION_JSON).build(); throw new WebApplicationException(response); // Throw new UnAuthor |
材料:
by 刘迎光@萤火虫工作室
OpenBI交流群:495266201
MicroService 微服务交流群:217722918
mail: liuyg#liuyingguang.cn
博主首页(==防止爬虫==):http://blog.liuyingguang.cn
本文主要使用jersey1.x,结合html5的FormData对象来进行多文件提交示例讲解
FormData对象是html5的一个对象,目前的一些主流的浏览器都已经兼容。IE8及以下是不支持FormData的。FormData还支持困扰众多开发者已久的ajax的上传文件,以前我们上传文件,需要写一个表单直接刷新提交,但是这里不需要了。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
function test(){
var form = new FormData(document.getElementById("form1"));
$.ajax({
url:"http://192.168.2.8:8066/file/multiFiles/",
type:"post",
data:form,
processData:false,
contentType:false,
success:function(data){
alert("pass");
},
error:function(e){
alert("错误!!");
}
});
}
</script>
</head>
<body>
<form id
本项目是将restful项目打包成可执行的war包,在docker中执行
docker 1.10.3
jetty 8
jersey 1.19
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<systemProperties>
</systemProperties>
<webApp>
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.