使用球衣上传文件上的 Http 415 [英] Http 415 on file Upload using jersey

查看:30
本文介绍了使用球衣上传文件上的 Http 415的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 RESTful 文件上传代码:

My code for RESTful file upload :

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

当我尝试使用 curl 上传文件时curl -X POST --form part=@file.txt url

When I try to upload a file using curl curl -X POST --form part=@file.txt url

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

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

推荐答案

在尝试了很多示例之后,最终在 http://iambigd.blogspot.com/2011/06/java-upload-file-using-jersey.html

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

@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());
        }
     }
   }
}
}

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

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

这篇关于使用球衣上传文件上的 Http 415的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆