SpringMVC文件上传

本文详细介绍了一种使用SpringMVC框架进行文件上传的方法。通过配置文件解析器和前端控制器,实现了文件的接收、处理及存储。代码示例清晰展示了如何设置文件上传路径,以及如何利用UUID确保文件名唯一性。
package wx;


import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(path="/file")
public class Fileupload {

//    @RequestMapping(path="/fileupload")
//    public String fileupload(HttpServletRequest request) throws Exception {
//        String path = request.getSession().getServletContext().getRealPath("/upload");
//        File file = new File(path);
//        if (!file.exists()) {
//            file.mkdirs();
//        }
//        DiskFileItemFactory factory = new DiskFileItemFactory();
//        ServletFileUpload fileUpload = new ServletFileUpload(factory);
//        List<FileItem> list = fileUpload.parseRequest(request);
//        for (FileItem fileItem : list) {
//            if (fileItem.isFormField()) {
//            } else {
//                String filename = fileItem.getName();
//                filename = UUID.randomUUID().toString().replace("-","").toUpperCase()+filename;
//                fileItem.write(new File(file, filename));
//                fileItem.delete();
//            }
//        }
//        return "success";
//    }

    //springMVC
    //1.配置文件解析器
    @RequestMapping(path="/fileupload")
    public String fileupload(HttpServletRequest request, MultipartFile upload) throws Exception {
        String path = request.getSession().getServletContext().getRealPath("/upload");
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String filename = upload.getOriginalFilename();
        System.out.println("文件名称:"+filename);
        filename = UUID.randomUUID().toString().replace("-","").toUpperCase()+filename;
        upload.transferTo(new File(path,filename));
        return "success";
    }
}

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="wx"/>

    <!-- 视图解析器对象 -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--前端控制器,哪些静态资源不拦截-->
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>

    <!--配置文件解析器对象-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760" />
        <property name="defaultEncoding" value="utf-8" />
    </bean>

    <!-- 开启SpringMVC框架注解的支持 -->
    <mvc:annotation-driven />

</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值