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

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

问题描述

我的RESTful文件上传代码:

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





$ b

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

我得到HTTP 415-不支持的媒体类型错误。什么是错误的?

解决方案

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



pre
@Path(/ simpleupload)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void simpleUpload(
// @ Context UriInfo ui,
@Context HttpServletRequest请求
){
String fileRepository =D: \\;
if(ServletFileUpload.isMultipartContent(request)){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
列表< FileItem> items = null;
尝试{
items = upload.parseRequest(request);
} catch(FileUploadException e){
e.printStackTrace();

if(items!= null){
Iterator< FileItem> iter = items.iterator(); (iter.hasNext()){
FileItem item = iter.next();
if(!item.isFormField()&& item.getSize()> 0){
System.out.println(File is found。);
String fileName = processFileName(item.getName());
尝试{
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());




$ b code $


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

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

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

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

解决方案

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

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

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

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