transferto转换文件报错

2022年6月4日07:36:01排错和经验评论245阅读模式

问题

上传文件到服务器,直接把File对象当作transferto()方法的参数;但是使用postman请求报错。报错信息如下:文章源自随机的未知-https://sjdwz.com/11202.html

java.io.FileNotFoundException: C:\Users\xxxxx\AppData\Local\Temp\盘符:\fileUpload\xxxxx.jpg(文件名、目录名或卷标语法不正确。),后面的路径是我保存的路径,前面的路径是transferto()方法自己加的。文章源自随机的未知-https://sjdwz.com/11202.html

之前的代码

截取部分上传文件的代码代码文章源自随机的未知-https://sjdwz.com/11202.html

    if(!file.isEmpty()) {
        String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/";
        File dir = new File(filePath);
        if(! dir.exists()) {
            dir.mkdir();
        }

        String path = filePath + file.getOriginalFilename();
        File tempFile = null;
        try {
            tempFile =  new File(path);

            file.transferTo(tempFile);
        }

代码解释

1)取得上产文件的path,加上upload 形成如: app directory/application/upload的路径并生成一个文件夹(没有的话)文章源自随机的未知-https://sjdwz.com/11202.html

2)在该文件夹下生成一个和上传文件同名的文件
3)将上传文件MultiPartFile transferto 到上述文件中文章源自随机的未知-https://sjdwz.com/11202.html

springBoot本地测试是没有问题的(内嵌tomcat server),但打包上传到服务器,出现了找不到文件的错误。文章源自随机的未知-https://sjdwz.com/11202.html

这个错误是file.transferTo(tempFile); 造成的,因为transferTo方法会默认在tempFile前添加一个新的路径,是一个没用的路径。这样一来,tempFile path是有的,但前边加一个路径后,就造成了找不到文件的错误。文章源自随机的未知-https://sjdwz.com/11202.html

解决方案

这里的解决办法是不用transferTo()方法,改用org.apache.commons.io.FileUtils工具类中的方法文章源自随机的未知-https://sjdwz.com/11202.html

首先在maven的pom.xml中添加commons-io commons-io 2.5的依赖
然后之前的代码改成:文章源自随机的未知-https://sjdwz.com/11202.html

    if(!file.isEmpty()) {
        String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/";
        File dir = new File(filePath);
        if(! dir.exists()) {
            dir.mkdir();
        }

        String path = filePath + file.getOriginalFilename();
        File tempFile = null;
        try {
            tempFile =  new File(path);
                    FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile);

这样就将multipartfile正常转到tempFile中了。文章源自随机的未知-https://sjdwz.com/11202.html

最一开始的代码如果传给transferto()方法一个File对象他会在前面加一个默认路径在你的File对象路径前面,transferto没有不带参的方法。上面那个方法直接导入依赖jar包,然后再把File对象的路径传入到FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile)方法中。文章源自随机的未知-https://sjdwz.com/11202.html

其他解决方案

transferto()方法还有一个文章源自随机的未知-https://sjdwz.com/11202.html

default void transferTo(Path dest) throws IOException, IllegalStateException {
FileCopyUtils.copy(this.getInputStream(), Files.newOutputStream(dest));
} 

还有一个这个方法,这个方法感觉跟上面的那个方法一样,我就研究了一下Path 引用类型文章源自随机的未知-https://sjdwz.com/11202.html

Path path1 = Paths.get(“D:”,“fileUpload”,fileName);
if (true){
//上传文件
System.out.println("=============="+dest);
file.transferTo(path1); //保存文件
//FileUtils.copyInputStreamToFile(file.getInputStream(), dest);
System.out.print(“文件的保存路径”+path1+"\n");
}

Path 引用类型有一个get(参数)方法这个方法可以获取路径,就可以把上传的路径写了进去,结果也可以上传文件。文章源自随机的未知-https://sjdwz.com/11202.html

文章源自随机的未知-https://sjdwz.com/11202.html
欢迎关注本站微信公众号:随机的未知 如果喜欢本文,欢迎点赞,收藏,转发,打赏。
  • 本文由 发表于 2022年6月4日07:36:01
  • 转载请注明:来源:随机的未知 本文链接https://sjdwz.com/11202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定