我尝试使用Jersey上传文件,但我遇到了很多麻烦。我看到我必须配置MultiPartFeature
以使事情发生但我无法使其发挥作用。我知道我必须在球衣上启用多部分功能,但这就是问题所在。
我尝试配置自定义应用程序和ResourceConfig
,但由于某种原因,当我这样做时,我的休息服务停止工作。我还尝试添加<init-param>
启用MultiPart,但也没有。
我正在使用Tomcat7,Spring,Jersey和Jackson
这些是我尝试过的几种方法。
org.glassfish.jersey upload file with FormDataContentDisposition
Uploading file using Jersey over RESTfull service and The resource configuration is not modifiable?
我使用默认的servlet配置javax.ws.rs.core.Application
来映射我的其余服务。我想这是整个问题,但正如我之前提到的,当我尝试映射自定义Application
ou ResourceConfig
时,我遇到了其他问题
这是我目前的配置。使用此配置,一切都很好,除了Multipart
的web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/not-found.html</location>
</error-page>
<context-param>
<param-name>STORAGE_PROPERTY</param-name>
<param-value>${user.home}/tec-imovel/STORAGE/property</param-value>
</context-param>
<context-param>
<param-name>STORAGE_USER</param-name>
<param-value>${user.home}/tec-imovel/STORAGE/user</param-value>
</context-param>
<filter>
<filter-name>ExpiresFilter</filter-name>
<filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
<init-param>
<param-name>ExpiresByType image</param-name>
<param-value>A2592000 </param-value>
</init-param>
<init-param>
<param-name>ExpiresByType text/css</param-name>
<param-value>A2592000</param-value>
</init-param>
<init-param>
<param-name>ExpiresByType application/javascript</param-name>
<param-value>A2592000</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ExpiresFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<!-- Spring Security Char Encoding Filter-->
<filter>
<filter-name>encoding-filter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Servlet declaration can be omitted in which case it would be automatically
added by Jersey-->
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<!-- Upload de imagens de usuario -->
<servlet>
<servlet-name>ImagemUsuarioUploadServlet</servlet-name>
<servlet-class>com.tecimovel.upload.servlet.ImagemUsuarioUploadServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ImagemUsuarioUploadServlet</servlet-name>
<url-pattern>/imagem-usuario-upload</url-pattern>
</servlet-mapping>
<!-- Upload de imagens de imovel -->
<servlet>
<servlet-name>ImagemImovelUploadServlet</servlet-name>
<servlet-class>com.tecimovel.upload.servlet.ImagemImovelUploadServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ImagemImovelUploadServlet</servlet-name>
<url-pattern>/imagem-imovel-upload</url-pattern>
</servlet-mapping>
<!-- Servlet de download de imagens do imovel -->
<servlet>
<servlet-name>DownloadImagemImovelServlet</servlet-name>
<servlet-class>com.tecimovel.download.servlet.DownloadImagemImovelServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DownloadImagemImovelServlet</servlet-name>
<url-pattern>/download-imagem-imovel/*</url-pattern>
</servlet-mapping>
<!-- Servlet de download de imagens do usuario -->
<servlet>
<servlet-name>DownloadImagemUsuarioServlet</servlet-name>
<servlet-class>com.tecimovel.download.servlet.DownloadImagemUsuarioServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DownloadImagemUsuarioServlet</servlet-name>
<url-pattern>/download-imagem-usuario/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/spring-security.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.web.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- Open Session View -->
<filter>
<filter-name>oemInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
<init-param>
<param-name>entityManagerFactory</param-name>
<param-value>entityManagerFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>oemInViewFilter</filter-name>
<url-pattern>/api/*</url-pattern>
</filter-mapping>
<resource-ref>
<description>PostgreSQL DataSource</description>
<res-ref-name>jdbc/TECIMOVELDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<filter>
<filter-name>crawlerFilter</filter-name>
<filter-class>com.tecimovel.filter.CrawlerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>crawlerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>WebResourceOptimizer</filter-name>
<filter-class>ro.isdc.wro.http.WroFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>WebResourceOptimizer</filter-name>
<url-pattern>/wro/*</url-pattern>
</filter-mapping>
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tec-imovel</groupId>
<artifactId>tec-imovel</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>tec-imovel</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>apache.snapshots</id>
<url>http://repository.apache.org/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-imaging</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.9</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.12.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.8</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.4</version>
<!-- <version>1.5.6</version> -->
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.springframework</groupId> -->
<!-- <artifactId>spring-context</artifactId> -->
<!-- <version>4.0.4.RELEASE</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.springframework</groupId> -->
<!-- <artifactId>spring-core</artifactId> -->
<!-- <version>4.0.3.RELEASE</version> -->
<!-- </dependency> -->
<!--LOGGING SPRING -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>ro.isdc.wro4j</groupId>
<artifactId>wro4j-core</artifactId>
<version>1.7.6</version>
</dependency>
<!-- Hibernate EHCache API -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- EHCache uses slf4j for logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0.1</version>
</dependency>
</dependencies>
<build>
<finalName>tec-imovel</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
<!-- <plugin> -->
<!-- <groupId>org.zeroturnaround</groupId> -->
<!-- <artifactId>jrebel-maven-plugin</artifactId> -->
<!-- <version>1.1.5</version> -->
<!-- <configuration> -->
<!-- <addResourcesDirToRebelXml>true</addResourcesDirToRebelXml> -->
<!-- <alwaysGenerate>true</alwaysGenerate> -->
<!-- </configuration> -->
<!-- <executions> -->
<!-- <execution> -->
<!-- <id>generate-rebel-xml</id> -->
<!-- <phase>process-resources</phase> -->
<!-- <goals> -->
<!-- <goal>generate</goal> -->
<!-- </goals> -->
<!-- </execution> -->
<!-- </executions> -->
<!-- </plugin> -->
</plugins>
</build>
<properties>
<jersey.version>2.6</jersey.version>
<hibernate.version>4.3.5.Final</hibernate.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
接收上传文件的服务
@Path("/migracao")
public class MigracaoRS {
@Autowired
private MigracaoGenericService migracaoGenericService;
@Autowired
private UsuarioController usuarioController;
//IF I DON'T COMMENT THIS METHOD THE SERVER DOESN'T EVEN START BECAUSE OF THE MULTIPART THING
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) {
migracaoGenericService.migrar(uploadedInputStream, usuarioController.getUsuarioLogado());
}
}
泽西servlet中的初始化参数。如果我添加此init-param,我的其他服务将停止工作:(
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
答案 0 :(得分:1)
是的,你有原始的servlet配置
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
允许通过类路径扫描注册类。一旦你添加了<init-param>
...provider.classnames
,你就会对泽西说:“不要注册你在类路径上找到的所有资源和提供者,我会自己注册”。
因此,为了解决这个问题,您可以添加<init-param>
来扫描项目包,或类路径上的任何其他包(如其他jar)。
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
the.packages.to.scan,
multiple.pacakges,
can.be.separated.by.comma
</param-value>
</init-param>
这与扫描类路径不同,但在大多数情况下,所有资源都应该在项目中,如果没有,只需显式注册jar包。
答案 1 :(得分:0)
我认为使用JSON作为使用者更容易,并将文件作为字节数组传递,这将使其成为json请求的一部分。