首页 > 解决方案 > Files.write(Path,byte[]) 上的 java.nio.channels.ClosedByInterruptException

问题描述

我需要下载附件文件(例如 jpeg、gif、pdf 等)我使用了 Files.write(Path,byte[]) 方法,但我得到了 ClosedByInterruptException

     public String downloadattachment(){
               try {            
                   headers.set("authorization", authorization);              
                   HttpEntity<String> entity = new HttpEntity<>(headers);            
                   ResponseEntity<byte[]> result = 
    restTemplate.exchange(attachmentUrl, 
                        HttpMethod.GET, entity, byte[].class);              
  String fileName = 
                result.getHeaders().getContentDisposition().getFilename();           
                String pathDirStr = applicationProperties.getLoggerPath() + 
                   File.separator + ticketId;            
                 SyncUtils.createDirIfNotExist(pathDirStr);                     
                 String pathFile = pathDirStr+File.separator+fileName;           
                 Files.write(Paths.get(pathFile), result.getBody());             
                 logger.debug("Attachment file downloaded in location : " + 
                  pathFile);            
                   resultPath=pathFile;         
              } catch (Exception e) { 
                    logger.debug("Exception occur while downloading attachment file.", e);

        } return resultPath; 
    }

痕迹:

"java.nio.channels.ClosedByInterruptException: null
        at java.nio.channels.spi.AbstractInterruptibleChannel.end(Unknown Source)
        at sun.nio.ch.FileChannelImpl.write(Unknown Source)
        at java.nio.channels.Channels.writeFullyImpl(Unknown Source)
        at java.nio.channels.Channels.writeFully(Unknown Source)
        at java.nio.channels.Channels.access$000(Unknown Source)
        at java.nio.channels.Channels$1.write(Unknown Source)
        at java.nio.file.Files.write(Unknown Source)
        at com.rogers.service.ComcastServiceImpl.downloadComcastAttachmentInDir(ComcastServiceImpl.java:564)

标签: javaresttemplate

解决方案


推荐阅读