简体   繁体   English

将文件从Web服务器传输到客户端

[英]Transfer file from web server to client

I have a simple Spring java web server. 我有一个简单的Spring Java Web服务器。 It does some processing that creates some xml files on the server. 它进行一些处理,在服务器上创建一些xml文件。 I would like to transfer those files to the client computer. 我想将这些文件传输到客户端计算机。

For some reason I do not even know how to start or search for an answer. 由于某种原因,我什至不知道如何开始或寻找答案。 It should not be hard because I am using Spring and they are only xml files not binary. 应该不难,因为我使用的是Spring,它们只是xml文件而不是二进制文件。 Can someone direct me of what I should be looking for in a search? 有人可以指示我在搜索中应该寻找的内容吗?

I am using @RequestMapping in my controllers. 我在控制器中使用@RequestMapping。 They look something this: 他们看起来像这样:

@Controller
@RequestMapping(value = "/export")
public class ExportController {


    @RequestMapping(method = RequestMethod.GET)
    public String getExport(ModelMap map) throws Exception {

    return "export"
}

This returns to a jsp file. 这将返回一个jsp文件。

The simplest (but not best) way would be to write them to disk and then to provide a link to them on your web page. 最简单(但不是最好)的方法是将它们写入磁盘,然后在您的网页上提供指向它们的链接。

The other way would be to open a response stream and write back the contents. 另一种方法是打开响应流并写回内容。

There are two ways: 有两种方法:

1) Controller Method: 1)控制器方式:
Specify controller 指定控制器

@RequestMapping("/getFile")
public ResponseEntity<byte[]> getFile() throws IOException {
    InputStream in = fetchFile("test.png");

    final HttpHeaders headers = new HttpHeaders();
    //Specify MediaType of the file you want to return
    headers.setContentType(MediaType.IMAGE_PNG); 

    return new ResponseEntity<byte[]>(IOUtils.toByteArray(in), headers, HttpStatus.CREATED);
}

And the mvc annotation in servlet-context.xml file: 并在servlet-context.xml文件中添加了mvc批注:

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>image/jpeg</value>
                    <value>image/png</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

2) Save file in the Server Context and expose a path to the browser. 2)将文件保存在服务器上下文中,并向浏览器公开一个路径。

This is what we have used in our project: 这是我们在项目中使用的:

    @RequestMapping(value = "/export", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ResponseBody
    public FileSystemResource downloadDocument(@PathVariable Long docId, HttpServletRequest request, 
            HttpServletResponse response) throws IOException, DocumentServiceException {                    
        //set the default file name to be saved by user. 
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition","attachment;filename=XMLFileName");
        return new FileSystemResource(new File("XMLfilePath"));

    }

Note that XMLFileName is the name of file which the user will save by default and XMLfilePath is the physical path of the file. 请注意, XMLFileName是用户将默认保存的文件名,而XMLfilePath是文件的物理路径。

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

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