与球衣2,maven和tomcat 7的空多部分请求

时间:2015-09-04 13:11:43

标签: java maven tomcat jersey multipart

我遇到项目迁移问题。 我使用 Netbeans 7.4 创建了一个Web应用程序,使用来自netbeans库和其他自制库的 Jersey 2 JSF 2 。 现在,我使用 maven 转移到 Netbeans 8.1 来管理库依赖项。新旧网络应用都使用 Tomcat 7

使用旧的源码,我使用multipart的webservices工作正常。对于新版本,请求似乎是空的。

以下是代码(旧版和新版的 SAME 代码,但新代码现在使用maven):

@POST
@Produces("application/pdf")
@Path("produce/{checksum}")
@RolesAllowed("admin")
public Response produce(@Context HttpServletRequest request, @PathParam("checksum") String checksum, @Context SecurityContext sc) {
    l.info(String.format("Produce request by user %s received at %s ", sc.getUserPrincipal().getName(), new Date()));

    try {
        File tempInputXml = File.createTempFile("input", ".xml");
        File tempZip = File.createTempFile("zip", ".zip");
        List<Part> parts = new ArrayList(request.getParts());
        l.info(String.format("%d parts", parts.size()));

        InputStream inputStream = null;
        if (parts.isEmpty()) {
            l.info("No part in the request, try other way");
            inputStream = request.getInputStream();
        } else {
            Part firstPart = parts.get(0);
            l.info(String.format("ContentType part 0: %s", firstPart.getContentType()));
            try (OutputStream os = new FileOutputStream(tempZip)) {
                IOUtils.copy(firstPart.getInputStream(), os);
            }
        }
    ...
    byte[] pdfContent = ....;
    return Response.ok(pdfContent).build();
}
对于新版本,

request.getParts().isEmpty()始终为true,而旧版本则为<dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>javax.servlet.jsp.jstl-api</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>com.googlecode.lambdaj</groupId> <artifactId>lambdaj</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.5.4</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.6</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.3.6</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.36</version> </dependency> <dependency> <groupId>org.primefaces</groupId> <artifactId>primefaces</artifactId> <version>3.5</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.2.7</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.11</version> </dependency> </dependencies>

这是新版本的pom.xml:

File temp = ...;
String md5 = md5Sum(temp);

String realUrl = "http://localhost:8080/soft/api/ws/produce/" + md5;
HttpPost httppost = new HttpPost(realUrl);

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("xml", temp);
HttpEntity multipart = builder.build();
httppost.setEntity(multipart);

HttpResponse response = httpclient.execute(httppost);

这是客户端代码(我不能遗憾地改变它)

FormDataMultiPart

当我尝试使用@Context HttpServletRequest时,客户端会收到415 HTTP错误。使用旧版本的PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE); if (psiFile instanceof PsiJavaFile) { PsiClass[] classes = ((PsiJavaFile) psiFile).getClasses(); // and so on } ,它工作正常

我的错误在哪里?

0 个答案:

没有答案