java 使用球衣上传文件的 Http 415

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10600013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:47:07  来源:igfitidea点击:

Http 415 on file Upload using jersey

javajspfile-uploadjersey

提问by rightskewed

My code for RESTful file upload :

我的 RESTful 文件上传代码:

@Path("/upload") 
@POST 
@Consumes("multipart/form-data") 
public String post(
    @FormDataParam("part") String s, 
    @FormDataParam("part") FormDataContentDisposition d) { 
    return s + ":" + d.getFileName(); 
}

When I try to upload a file using curl curl -X POST --form [email protected] url

当我尝试使用 curl curl -X POST --form [email protected] url 上传文件时

I am getting a HTTP 415-Unsupported Media Type Error. What is wrong ?

我收到 HTTP 415 不支持的媒体类型错误。怎么了 ?

回答by Deepak Bala

This can happen due to a couple of reasons. I managed to narrow down some of them.

这可能由于几个原因而发生。我设法缩小了其中的一些范围。

  1. Your Content-Type header does not match with the one provided by the @Consumes header. Verify this with a proxy.

  2. You managed to stumble upon a bugthat was fixed in Jersey 1.4 related to the FormDataParam annotation.

  3. You included jersey-bundle and jersey-server et all in the same binary and they are competing against each other.

  4. You are using @FormParam instead of @FormDataParam.

  5. Your @FormDataParam is unrecognized by the introspection API because of conflicts with jersey-multipart and other jersey jars. If one jar is of version 1.x make sure the other jars are on the same version. While debugging the jersey API code I noticed that these method annotations turn up blank (on jersey's code) if the jar versions are not uniform. All method parameters on the REST service are replaced by the body content of the POST request irrespective of which FormDataParam they are supposed to contain.

  1. 您的 Content-Type 标头与 @Consumes 标头提供的标头不匹配。使用代理验证这一点。

  2. 您设法偶然发现Jersey 1.4 中修复的与 FormDataParam 注释相关的错误

  3. 您将 jersey-bundle 和 jersey-server 等都包含在同一个二进制文件中,并且它们相互竞争。

  4. 您正在使用@FormParam 而不是@FormDataParam。

  5. 由于与 jersey-multipart 和其他 jersey jar 冲突,您的 @FormDataParam 无法被内省 API 识别。如果一个 jar 的版本为 1.x,请确保其他 jar 的版本相同。在调试球衣 API 代码时,我注意到如果 jar 版本不统一,这些方法注释会变成空白(在球衣的代码上)。REST 服务上的所有方法参数都被 POST 请求的正文内容替换,而不管它们应该包含哪个 FormDataParam。

回答by Sllouyssgort

After trying a lot of examples finaly find the realy working example on http://iambigd.blogspot.com/2011/06/java-upload-file-using-jersey.html

在尝试了很多示例之后,最终在http://iambigd.blogspot.com/2011/06/java-upload-file-using-jersey.html上找到了真正有效的示例

@POST
@Path("/simpleupload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void simpleUpload(
    //@Context UriInfo ui,
    @Context HttpServletRequest request
){
    String fileRepository = "D:\";
    if (ServletFileUpload.isMultipartContent(request)) {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = null;
    try {
        items = upload.parseRequest(request);
    } catch (FileUploadException e) {
        e.printStackTrace();
    }
    if (items != null) {
        Iterator<FileItem> iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = iter.next();
            if (!item.isFormField() && item.getSize() > 0) {
            System.out.println("File is found.");
            String fileName = processFileName(item.getName());
            try {
                String savePath = fileRepository + fileName;
                System.out.println("savePath:" + savePath);
                item.write(new File(savePath));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            System.out.println("getFieldName:" + item.getFieldName());
            System.out.println(item.getString());
        }
     }
   }
}
}

(need the servlet-api.jar, (apache) commons-oi.jar and (apache) commons-fileupload.jar)

(需要 servlet-api.jar、(apache)commons-oi.jar 和(apache)commons-fileupload.jar)

回答by MADHAIYAN M

Please make sure you have mimepull.jaron the classpath

请确保类路径上有mimepull.jar

回答by Harald

You may need to register the MultipartFeatureas described in the Jersey documentation, chapter 8.3.1.2 Registration.

您可能需要按照 Jersey 文档第8.3.1.2注册中的说明注册MultipartFeature

Create a class something like this:

创建一个类似这样的类:

/**
 * 
 */
package com.verico.multipart.app;

import javax.ws.rs.ApplicationPath;

import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("/")
public class MultiPartApp extends ResourceConfig {

public MultiPartApp() {
    super(MultiPartFeature.class);
    }
}

And add the following init-param to your Jersey servlet in web.xml:

并将以下 init-param 添加到 web.xml 中的 Jersey servlet:

     <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.verico.multipart.app.MultiPartApp</param-value>
    </init-param>

回答by Camille R

Have you tried with an Input stream ?

你试过输入流吗?

Like:

喜欢

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA) 
public Response post(
        @Context HttpServletRequest request,
        @Context HttpHeaders headers, 
        @FormDataParam("file") InputStream fileStream,

Works fine for me.

对我来说很好用。