文件上传

Spring MVC 文件上传配置与实现

需要的jar包

1.commons io.jar

2.commons-fileupload.jar

springmvc.xml

7925105-30e08a5386adba6b.png
image.png

        id必须是multipartResolver
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        
              <!--  最大上传容积之和  默认字节-->
        <property name="maxUploadSize" value="20000000"></property>
        
        <!-- 默认编码方式设置成utf-8 -->
        <property name="defaultEncoding" value="utf-8"></property>
        
                <!-- 最大缓冲大小 -->
        <property name="maxInMemorySize" value="2048"></property>
        
                <!-- 懒加载模式 -->
        <property name="resolveLazily" value="true"></property>
        
                <!-- 文件上传临时目录,上传完成后,就会将临时文件删除; -->
        <property name="uploadTempDir" value=""></property>
       
            <!-- 跟maxUploadSize差不多,不过maxUploadSizePerFile是限制每个上传文件的大小,而maxUploadSize是限制总的上传文件大小 -->
        <property name="maxUploadSizePerFile" value=""></property>
        
                <!-- 保存文件名 -->
        <property name="preserveFilename" value=""></property>
    </bean> 

Controller

    @PostMapping("upload")
    public String upload(MultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException {
        
        if(file.isEmpty())
        {
            return "upload";
        }
        else
        {
            
            String path =  request.getServletContext().getRealPath("/file/");
            String filename = file.getOriginalFilename();
            File filepath = new File(path,filename);
            if(!filepath.getParentFile().exists())
            {
                filepath.getParentFile().mkdirs();
            }
            
            
            /**
             * 获取类型  .*
             */
        //  String ext = FilenameUtils.getExtension(filename);
            
            /**
             *  创建新名字
             */
        
        //  String newfileName = UUID.randomUUID().toString().replaceAll("-","")+"."+ext;
            
            String newPath= path+File.separator+filename;
            
            System.out.println("文件路径:"+newPath);
            
            file.transferTo(new File(newPath));
        
        }
        
        return "success";   
    }

优化下

新建一个模型类 Fileinfo

import java.io.Serializable;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@Component
public class FileInfo implements Serializable {
    private static final long serialVersionUID = 1L;
    private MultipartFile file;

    public MultipartFile getFile() {
        return file;
    }

    public void setFile(MultipartFile file) {
        this.file = file;
    }

}



Controller

    
    @PostMapping("upload")
    public String upload(@ModelAttribute FileInfo fileInfo,HttpServletRequest request,Model model) throws IllegalStateException, IOException {
        
        if(fileInfo.getFile().isEmpty())
        {
            return "upload";
        }
        else
        {
            
            String path =  request.getServletContext().getRealPath("/file");
            String filename = fileInfo.getFile().getOriginalFilename();
            File filepath = new File(path,filename);
            if(!filepath.getParentFile().exists())
            {
                filepath.getParentFile().mkdirs();
            }
            
            
            /**
             * 获取类型  .*
             */
        //  String ext = FilenameUtils.getExtension(filename);
            
            /**
             *  创建新名字
             */
        
        //  String newfileName = UUID.randomUUID().toString().replaceAll("-","")+"."+ext;
            
            String newPath= path+File.separator+filename;
            
            System.out.println("文件路径:"+newPath);
            
            fileInfo.getFile().transferTo(new File(newPath));
            
            model.addAttribute("fileinfo", fileInfo);
        }
        
        return "download";  
    }
    
    
    

前端

    <form action="upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file"/> 
        <input type="submit" value="上传">
    </form>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nier6088

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值