【问题标题】:Uploading file using Spring REST API使用 Spring REST API 上传文件
【发布时间】:2013-04-01 07:10:21
【问题描述】:

我有这样的控制器,我正在使用 POSTMAN REST 客户端上传 pdf 文件。设置内容类型:multipart/form-data;boundary=randomBoundaryNotInAnyOfParts 它正在创建文件,但没有在其中写入任何内容。我在这里错过了什么吗?

@RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
public @ResponseBody String upload(HttpServletRequest request) {


InputStream is = null;
OutputStream out = null; 
try {

is = request.getInputStream();
byte[] b = new byte[1024];
out = new FileOutputStream(new File("C:\\tmp\\upload.txt"));
out.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally{

try{
if(out != null){
out.close();
}
if(is != null){
is.close();
}
}catch(IOException io){
io.printStackTrace();
}

}
return null;
}

谢谢, 维奈

【问题讨论】:

    标签: spring rest file-upload


    【解决方案1】:

    你的 byte[] 是空的,因此它没有在文件中写入任何内容。获取请求内容到byte[],然后写入文件。

    或者你可以使用 BufferedWriter 也更快。

    谢谢, 布里杰什

    【讨论】:

      猜你喜欢
      • 2018-04-04
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多