【发布时间】:2015-11-24 08:48:18
【问题描述】:
我必须通过 java 代码和 Rest web 服务将文件从服务器(从它的文件系统)发送到 Cliente(另一台电脑并将文件存储在一个特定的文件夹中)。这些文件甚至可以是 120MB。
我使用MultipartFile 从我的网页上传文件,但我不知道如何从客户端下载。
最好使用 REST Web 服务来返回文件和消息以及方法的结果(如果有错误,则为 true 或 false)。
你有想法吗?
目前我在服务器中使用此代码:
最好的办法是
@Override
@RequestMapping(value = "/oldmethod", method = RequestMethod.GET)
public @ResponseBody Response getAcquisition(@RequestParam(value="path", defaultValue="/home") String path){
File file;
try {
file = matlabClientServices.getFile(path);
if (file.exists()){
InputStream inputStream = new FileInputStream(path);
byte[]out=org.apache.commons.io.IOUtils.toByteArray(inputStream);
return new Response(true, true, out, null);
}
else
return new Response(false, false, "File doesn't exist!", null);
} catch (Exception e) {
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in MatlabClientControllerImpl::getAcquisition :" + errorResponse.getStacktrace());
return new Response(false, false, "Error during file retrieving!", errorResponse);
}
}
但对客户来说,下面的代码不起作用:
public Response getFileTest(@RequestParam(value="path", defaultValue="/home") String path){
RestTemplate restTemplate = new RestTemplate();
Response response = restTemplate.getForObject("http://localhost:8086/ATS/client/file/oldmethod/?path={path}", Response.class, path);
if (response.isStatus() && response.isSuccess()){
try {
Files.write(Paths.get("PROVIAMOCI.txt"),org.apache.commons.io.IOUtils.toByteArray(response.getResult().toString()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return response;
}
它写入 byte[] 字符而不是原始文本
【问题讨论】:
-
什么样的问题给你?一些错误......
-
是的,我无法检索原始文件
标签: java spring file rest file-io