相关疑难解决方法(0)

从弹簧控制器下载文件

我有一个要求,我需要从网站上下载PDF.PDF需要在代码中生成,我认为这将是freemarker和像iText这样的PDF生成框架的组合.有更好的方法吗?

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

java spring controller file download

354
推荐指数
9
解决办法
41万
查看次数

Spring Rest Web服务将文件作为资源返回

我正在尝试从服务器上部署的Rest Web服务返回文件流,并在客户端上从Rest Web服务处理此流.在服务器上我使用此代码:

@Override
@RequestMapping(value = "/file", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) 
public @ResponseBody Resource getAcquisition(@RequestParam(value="filePath", required = true) String filePath) throws FileNotFoundException{
    // acquiring the stream
    File file= new File(filePath);
    InputStream stream = new FileInputStream(file);
    // counting the length of data
    final long contentLength = file.length() ;

    return new InputStreamResource(stream){
        @Override
        public long contentLength() throws IOException {
            return contentLength;
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

而且,此刻,在客户端我使用它(然后我必须在文件系统上写文件)

@Override
public void getFile(String serverIp, String toStorePath, String filePath) throws Exception{
    RestTemplate restTemplate = new …
Run Code Online (Sandbox Code Playgroud)

java rest spring file spring-mvc

6
推荐指数
1
解决办法
1万
查看次数

为 Spring Boot 实现字节服务

我想使用 Spring Boot Rest API 在 Angular 中实现视频播放器。我可以播放视频,但我无法进行视频搜索。每次当我使用 Chrome 或 Edge 时,视频都会一遍又一遍地播放。

我试过这个端点:

@RequestMapping(value = "/play_video/{video_id}", method = RequestMethod.GET)
    @ResponseBody public ResponseEntity<byte[]> getPreview1(@PathVariable("video_id") String video_id, HttpServletResponse response) {
        ResponseEntity<byte[]> result = null;
        try {
            String file = "/opt/videos/" + video_id + ".mp4";
            Path path = Paths.get(file);
            byte[] image = Files.readAllBytes(path);

            response.setStatus(HttpStatus.OK.value());
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentLength(image.length);
            result = new ResponseEntity<byte[]>(image, headers, HttpStatus.OK);
        } catch (java.nio.file.NoSuchFileException e) {
            response.setStatus(HttpStatus.NOT_FOUND.value());
        } catch (Exception e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
        return result;
    } …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-boot angular

4
推荐指数
1
解决办法
683
查看次数

标签 统计

java ×3

spring ×3

file ×2

spring-mvc ×2

angular ×1

controller ×1

download ×1

rest ×1

spring-boot ×1