spring ResourceLoader如何从目录中查看pdf文件

时间:2016-08-16 04:11:06

标签: java spring spring-boot

path = uploads / files / 38c84b59-101b-4606-8fe7-3328f1871c03.pdf

@RequestMapping(method = RequestMethod.GET, value = "/explorer/file/{id:.+}")
        @ResponseBody
        public ResponseEntity<?> getFile(@PathVariable Integer id) {
            String filename = explorerService.getById(id).getPath();
            try {
                return ResponseEntity.ok(resourceLoader.getResource("file:" + filename));
            } catch (Exception e) {
                return ResponseEntity.notFound().build();
            }
        }

而不是pdf,它提供了一些文字

提前多多感谢

screenshot

1 个答案:

答案 0 :(得分:2)

将主要问题的评论纳入正式答案。重要的是,浏览器正在发送接受:public class TreeNode<T> { private T value; private bool hasParent; public TreeNode<T> parent; private List<TreeNode<T>> children; public TreeNode(T value, TreeNode<T> parent) { this.parent = parent; if (value == null) { throw new ArgumentNullException( "Cannot insert null value!"); } this.value = value; this.children = new List<TreeNode<T>>(); } public T Value { get { return this.value; } set { this.value = value; } } public int ChildrenCount { get { return this.children.Count; } } public class Tree<T> { // The root of the tree private TreeNode<T> root; public Tree(T value) { if (value == null) { throw new ArgumentNullException( "Cannot insert null value!"); } this.root = new TreeNode<T>(value,null); } public Tree(T value, params Tree<T>[] children) : this(value) { foreach (Tree<T> child in children) { this.root.AddChild(child.root); } } 或至少*/*,否则会在请求中出错。

application/pdf