简体   繁体   English

使用Spring和Rest Web服务将文件从服务器发送到Client Java

[英]Send file from Server to Client java with Spring and Rest web service

I have to send file from Server (from its file system) to Cliente (another pc and store file in a particularly folder) through java code and Rest web service. 我必须通过Java代码和Rest Web服务将服务器中的文件(从其文件系统)发送到Cliente(另一台PC并将文件存储在特定的文件夹中)。 These files can be even 120MB . 这些文件甚至可以是120MB I used MultipartFile to upload file from my web page but I don't know how to download from the client. 我使用MultipartFile从网页上载文件,但我不知道如何从客户端下载文件。 It would be good the possibility to use a REST web service that returns both file and message with result of method (true or false if there was an error). 最好使用一种REST Web服务,该服务同时返回文件和消息以及方法的结果(如果有错误,则为true或false)。 Do you have an idea? 你有想法吗? At the moment I use this code in server: 目前,我在服务器中使用以下代码:

and the best way would be 最好的方法是

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

but to the client the code below doesn't work: 但对客户而言,以下代码不起作用:

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

it writes the byte[] characters and not the original text 它写入byte []字符而不是原始文本

As per my understanding FileSystemResource used for get the file system using file system URI but it is not over the HTTP. 根据我的理解,FileSystemResource用于使用文件系统URI来获取文件系统,但它不是通过HTTP进行的。 Using FileSystemResource you can access files from your local machine to local accessible file system and from server to your server accesiable file system. 使用FileSystemResource,您可以将文件从本地计算机访问到本地可访问文件系统,也可以从服务器访问服务器可访问的文件系统。 But could not access from local to server file system. 但是无法从本地访问服务器文件系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM