程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从Spring Controller下载文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决从Spring Controller下载文件?@H_696_1@ 开发过程中遇到从Spring Controller下载文件的问题如何解决?下面主要结合日常开发的经验,给出你关于从Spring Controller下载文件的解决方法建议,希望对你解决从Spring Controller下载文件有所启发或帮助;
@requestMapPing(value = "/files/{file_name}", method = requestMethod.GET)
public voID getfile(
    @PathVariable("file_name") String filename, 
    httpServletResponse responsE) {
    try {
      // get your file as inputStream
      inputStream is = ...;
      // copy it to response's OutputStream
      org.apache.commons.io.IoutIls.copy(is, response.getoutputStream());
      response.flushBuffer();
    } catch (IOException eX) {
      log.info("Error wriTing file to output stream. filename was '{}'", filename, eX);
      throw new RuntimeException("IOError wriTing file to output stream");
    }

}

一般来说,当你拥有时response.getoutputStream(),你可以在其中编写任何内容。你可以将此输出流作为将生成的pdf放置到生成器的地方。另外,如果你知道要发送的文件类型,则可以设置

response.setContentType("application/pdf");

通过使用Spring的resourcehttpmessageConverter中的内置支持,我能够简化这一过程。如果可以确定R_419_11845@ime类型,则将设置content-length和content-type

@requestMapPing(value = "/files/{file_name}", method = requestMethod.GET)
@ResponseBody
public FileSystemresource getfile(@PathVariable("file_name") String fileName) {
    return new FileSystemresource(myservice.getfileFor(fileName)); 
}

解决方法@H_696_1@

我有一个需要从网站下载PDF的要求。PDF需要在代码中生成,我认为这将是freemarker和iText等PDF生成框架的结合。还有更好的办法吗?

但是,我的主要问题是如何允许用户通过Spring Controller下载文件?

大佬总结

以上是大佬教程为你收集整理的从Spring Controller下载文件全部内容,希望文章能够帮你解决从Spring Controller下载文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。