首页 > 代码库 > SpirngMVC文件上传

SpirngMVC文件上传

Spirng文件上传

定义配置System.properties文件的文件上传路径

fileSaveDir=E:/ceshi/

 

 

<!--SpringMVC配置-->

<!--读取文件上传资源文件-->
<context:property-placeholder location="classpath:system.properties"/>

 

SpringMVC文件上传<!-- 多部分文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- 上传文件的大小,单位为字节 -->
    <property name="maxUploadSize" value="http://www.mamicode.com/104857600" />
    <property name="maxInMemorySize" value="http://www.mamicode.com/4096" />
    <!-- 请求的编码格式 -->
    <property name="defaultEncoding" value="http://www.mamicode.com/UTF-8"></property>
    <!-- 上传文件的临时路径 -->
    <property name="uploadTempDir" value="http://www.mamicode.com/fileUpload/temp"></property>

 


</bean>

 

1.页面部分

<body>

    <form action="/Update" method="post" enctype="multipart/form-data">
        <center>
            <h2>文件上传</h2>
            用户姓名:<input name="username"/><br/>
            用户头像:<input type="file" name="userhead"><br/>
            用户头像:<input type="file" name="userhead"><br/>
            用户头像:<input type="file" name="userhead"><br/>
        <input type="submit" value="http://www.mamicode.com/提交"/>
        </center>
    </form>

</body>

3.文件上传控制器

@Controller
public class UpdateController {

    private Logger logger = LogManager.getLogger(UpdateController.class);

    //获取资源文件下的定义的地址
    @Value("${fileSaveDir}")
    private String fileSaveDir;
    
    @RequestMapping("/Update")
    public String upfile(String username, MultipartFile[] userhead ){

        logger.debug("这是fileSaceDir"+fileSaveDir);
        for (MultipartFile file:userhead) {
            logger.debug("username是"+username+"userhead是"+userhead);
            //自动生成动态码
            String filename=UUID.randomUUID().toString();
            logger.debug("uuid是"+filename);

            //存入Map集合中
            Const.fileMap.put(filename,file.getOriginalFilename());

            try {
                file.transferTo(new File(fileSaveDir+file.getOriginalFilename()));
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
             return "succ";
    }

 

设置文件上传名称

public class Const {
    public static Map<String,String> fileMap = new HashMap<String, String>();

    //模拟数据库存储如map中
    static {
        fileMap.put("6dc0d57c-1ee2-4a35-8e95-817d02ad5667","1.jpg");
        fileMap.put("b8d74873-797d-458d-93dd-14fe3122bb55","2.jpg");
        fileMap.put("c4cf5385-cb4a-48a8-a763-b9957ffbb807","3.jpg");

 

文件的下载

/获取资源文件下的定义的地址

   @Value("${fileSaveDir}")
   private String saveDir;

   @RequestMapping("/downFile")
   public void downFile(String filename, HttpServletResponse resp) throws IOException {
       resp.setContentType("application/octet-stream; charset=utf-8");

       // 处理中文文件名中文乱码问题

           String realOriginalFilename = Const.fileMap.get(filename);
           String name = new String(realOriginalFilename.getBytes("utf-8"), "ISO-8859-1");
           resp.setHeader("Content-Disposition", "attachment; filename=" + name);
           IOUtils.copy(new FileInputStream(new File(saveDir, filename)), resp.getOutputStream());

SpirngMVC文件上传