【问题标题】:Jar executable of spring app file upload failed?spring app文件上传的jar可执行文件失败?
【发布时间】:2021-02-07 03:10:07
【问题描述】:

这个spring app执行简单的文件上传,

这是控制器类

    @Override
        public String fileUpload(MultipartFile file) {
    
            try{
                
                // save uploaded image to images folder in root dir
                Files.write(Paths.get("images/"+ file.getOriginalFilename()), file.getBytes());
                
                // perform some tasks on image 
                return "";
    
            } catch (IOException ioException) {
                
                return "File upload has failed.";
            } finally {
                Files.delete(Paths.get("images/" + file.getOriginalFilename()));
            }
        }

但是当我构建 jar 并运行时,它会抛出 IOException 说, java.nio.file.NoSuchFileException: images\8c9.jpeg.

所以我的问题是如何在 jar 可执行文件本身中添加图像文件夹。

谢谢。

【问题讨论】:

  • 您在可执行 jar 所在的位置是否有目录“images”?或者您可能需要设置完整路径
  • @guchuan,是的,但是我可以在 jar 中添加那个文件夹吗?
  • 您的源代码项目是什么样的?如果它位于类路径资源下,您可以尝试“classpath:images/...”
  • “罐子内部”不可写。您需要将图像存储在外部;常见选项包括文件服务器上的网络共享、将它们填充到数据库中或使用像 S3 这样的对象存储。
  • 当然,这工作对于试用来说还不错,但请记住,(1) 临时目录的空间可能有限,并且 (2) 它是临时的。

标签: java spring file-upload jar


【解决方案1】:

您应该提供图片文件夹的完整路径,或者先创建图片文件夹保存在java.io.tmpdir

但是,在我看来,您应该从配置文件中配置上传文件夹以获得灵活性。看看这个。

app:
   profile-image:
      upload-dir: C:\\projs\\web\\profile_image
      file-types: jpg, JPG, png, PNG
      width-height: 360, 360
      max-size: 5242880

在您的服务或控制器中,做任何您喜欢的事情,可能是验证图像类型、大小等,并根据您的需要处理它。例如,如果你想要缩略图(或头像..)。

在你的控制器或服务类中,获取目录:

@Value("${app.image-upload-dir:../images}")
private String imageUploadDir;

最后,

public static Path uploadFileToPath(String fullFileName, String uploadDir, byte[] filecontent) throws IOException {
    Path fileOut = null;
    try{
        Path fileAbsolutePath = Paths.get(StringUtils.join(uploadDir, File.separatorChar, fullFileName));
        fileOut = Files.write(fileAbsolutePath, filecontent);
    }catch (Exception e) {
        throw e;
    }
    return fileOut; //full path of the file
}

对于您在评论中的问题:您可以使用java.io.File.deleteOnExit() 方法,该方法在虚拟机终止时删除抽象路径名定义的文件或目录。不过要小心,如果处理不当,可能会留下一些文件。

try (ByteArrayOutputStream output = new ByteArrayOutputStream();){
    URL fileUrl = new URL(url);
    String tempDir = System.getProperty("java.io.tmpdir");
    String path = tempDir + new Date().getTime() + ".jpg"; // note file extension 
    java.io.File file = new java.io.File(path); 
    file.deleteOnExit();
    
    inputStream = fileUrl.openStream();
    ByteStreams.copy(inputStream, output); // ByteStreams - Guava
    outputStream = new FileOutputStream(file);
    output.writeTo(outputStream);
    outputStream.flush();
    return file;
} catch (Exception e) {
    throw e;
} finally {
    try {
    if(inputStream != null) {
        inputStream.close();
    }
    if(outputStream != null) {
        outputStream.close();
    }
    } catch(Exception e){
        //skip
    }
}

【讨论】:

  • 文件处理完可以删除,在finally块里面
  • 处理是什么意思?如果你不需要它,为什么要下载它?如果文件不是太大,将其内容读入字节缓冲区
  • 哦...这是用于 OCR 的事情,一旦文本被提取,图像应该被删除,
猜你喜欢
  • 2023-03-11
  • 2012-01-01
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
  • 2012-04-03
  • 1970-01-01
相关资源
最近更新 更多