简体   繁体   English

从Spring MVC控制器返回xml文件

[英]Return xml file from spring MVC controller

I have tried a lot to return a file from the controller function. 我已经做了很多尝试从控制器功能返回文件。

This is my function: 这是我的功能:

@RequestMapping(value = "/files", method = RequestMethod.GET)
@ResponseBody public FileSystemResource getFile() {
     return new FileSystemResource(new File("try.txt")); 
}

I got this error message: 我收到此错误消息:

Could not write JSON: 无法编写JSON:
No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer 未找到类java.io.FileDescriptor的序列化程序,也未找到创建BeanSerializer的属性
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS))
(through reference chain: (通过参考链:
org.springframework.core.io.FileSystemResource[\\"outputStream\\"]->java.io.FileOutputStream[\\"fd\\"]); org.springframework.core.io.FileSystemResource [\\“ outputStream \\”]-> java.io.FileOutputStream [\\“ fd \\”]);
nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer 嵌套的异常是com.fasterxml.jackson.databind.JsonMappingException:未找到类java.io.FileDescriptor的序列化器,也未发现创建BeanSerializer的属性
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS))
(through reference chain: org.springframework.core.io.FileSystemResource[\\"outputStream\\"]->java.io.FileOutputStream[\\"fd\\"]) (通过参考链:org.springframework.core.io.FileSystemResource [\\“ outputStream \\”]-> java.io.FileOutputStream [\\“ fd \\”])

Does anyone have an idea how to solve it? 有人知道如何解决吗?

And, how should I send from the client (JavaScript, jQuery)? 而且,我应该如何从客户端(JavaScript,jQuery)发送?

EDIT 2: First of all - see edit 1 in the bottom - that't the right way to do it. 编辑2:首先-请参阅底部的编辑1-这不是正确的方法。 However, if you can't get your serializer to work, you can use this solution, where you read the XML file into a string, and promts the user to save it: 但是,如果无法使序列化器正常工作,则可以使用以下解决方案,在该解决方案中,您将XML文件读入字符串,并提示用户保存它:

@RequestMapping(value = "/files", method = RequestMethod.GET)
public void saveTxtFile(HttpServletResponse response) throws IOException {

    String yourXmlFileInAString;
    response.setContentType("application/xml");
    response.setHeader("Content-Disposition", "attachment;filename=thisIsTheFileName.xml");

    BufferedReader br = new BufferedReader(new FileReader(new File(YourFile.xml)));
    String line;
    StringBuilder sb = new StringBuilder();

    while((line=br.readLine())!= null){
        sb.append(line);
    }

    yourXmlFileInAString  = sb.toString();

    ServletOutputStream outStream = response.getOutputStream();
    outStream.println(yourXmlFileInAString);
    outStream.flush();
    outStream.close();
}

That should do the job. 那应该做的。 Remember, however, that the browser caches URL contents - so it might be a good idea to use a unique URL per file. 但是请记住,浏览器会缓存URL内容-因此,最好在每个文件中使用唯一的URL。

EDIT: 编辑:

After further examination, you should also just be able to add the following piece of code to your Action, to make it work: 经过进一步检查,您还应该能够将以下代码添加到Action中,以使其起作用:

response.setContentType("text/plain");

(Or for XML) (或用于XML)

response.setContentType("application/xml");

So your complete solution should be: 因此,完整的解决方案应该是:

@RequestMapping(value = "/files", method = RequestMethod.GET)
@ResponseBody public FileSystemResource getFile(HttpServletResponse response) {
    response.setContentType("application/xml");
    return new FileSystemResource(new File("try.xml")); //Or path to your file 
}

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

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