REST文件上传在Spring控制器使用的multipart / form-data的 [英] REST fileupload in Spring controller using multipart/form-data

查看:3052
本文介绍了REST文件上传在Spring控制器使用的multipart / form-data的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能上载其他数据(如描述等)使用的multipart / form-data的文件?我使用Backbone.js的在我的前端和我称之为REST API与它(jQuery的)。
我不使用任何视图解析器,但我想以某种方式通过我的文件到控制器,如:

  @RequestMapping(值=/图像/上传,方法= RequestMethod.POST)
公共字符串上传(UploadItem uploadItem,HttpSession的会议)

让uploadItem专卖店:

 私人字符串desctiption;
私人列表< CommonsMultipartFile> FILEDATA;

但我不添加(我不能)这我的模型。

当然,我也有兴趣,如果有可能有类似的控制器:

  @RequestMapping(值=/图像/上传,方法= RequestMethod.POST)
公共字符串上传(someFileType uploadItem,字符串desctiption)


解决方案

是的,你也可以用多声部数据一起传给其他表单字段。您可以检查的字段名称,并用它喜欢。 (如果(item.getName()。等于(desctiption)))。请参阅完整的例子
文件上传实例

  {尝试
    //解析请求
    列表项= upload.parseRequest(请求);
    迭代器迭代器= items.iterator();
    而(iterator.hasNext()){
     的FileItem项目=(的FileItem)iterator.next();
     如果(item.isFormField()及!&放大器;!item.getName()等于()。){
      字符串文件名= item.getName();
      串根= context.getRealPath(/);
      文件路径=新的文件(根+/上传);
      如果(!path.exists()){
       布尔状态= path.mkdirs();
      }      文件UploadedFile的=新的文件(路径+/+文件名);
      fileNames.add(文件名);
      的System.out.println(文件路径: -
        + uploadedFile.getAbsolutePath());      item.write(UploadedFile的);
     }
    }
   }赶上(FileUploadException E){
    的System.out.println(FileUploadException: - + e.getMessage());
   }赶上(例外五){
    的System.out.println(例外: - + e.getMessage());
   }
  }

Is it possible to upload file with additional data (like description etc.) with use of multipart/form-data? I'm using backbone.js in my frontend and I call REST api with it (jQuery). I don't use any view resolver but I want to somehow pass my file to controller like:

@RequestMapping(value = "/image/upload", method = RequestMethod.POST)
public String upload(UploadItem uploadItem, HttpSession session)

so that uploadItem stores:

private String desctiption;
private List<CommonsMultipartFile> fileData;

But I'm not adding (and I can't) this to my model.

Of course I'm also interested if it's possible to have controller like:

@RequestMapping(value = "/image/upload", method = RequestMethod.POST)
public String upload(someFileType uploadItem, String desctiption)

解决方案

Yes you can pass the other form fields also along with multi part data. You can check the field name and use it like. (if (item.getName().equals("desctiption"))). Refer complete example File Upload Example

try {
    // Parse the request
    List items = upload.parseRequest(request);
    Iterator iterator = items.iterator();
    while (iterator.hasNext()) {
     FileItem item = (FileItem) iterator.next();
     if (!item.isFormField() && !item.getName().equals("")) {
      String fileName = item.getName();
      String root = context.getRealPath("/");
      File path = new File(root + "/uploads");
      if (!path.exists()) {
       boolean status = path.mkdirs();
      }

      File uploadedFile = new File(path + "/" + fileName);
      fileNames.add(fileName);
      System.out.println("File Path:-"
        + uploadedFile.getAbsolutePath());

      item.write(uploadedFile);
     }
    }
   } catch (FileUploadException e) {
    System.out.println("FileUploadException:- " + e.getMessage());
   } catch (Exception e) {
    System.out.println("Exception:- " + e.getMessage());
   }
  }

这篇关于REST文件上传在Spring控制器使用的multipart / form-data的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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