码迷,mamicode.com
首页 > Web开发 > 详细

文件上传

时间:2020-05-03 21:48:26      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:frame   NPU   random   mapping   ring   nal   文件夹   current   div   

package com.sxt.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.sxt.utils.RandomStrUtils;

@Controller
@RequestMapping("upload")
public class FileUploadController {

    
    /**
     * 文件上传1
     */
    @RequestMapping("upload01")
    public String upload01(MultipartFile mf,HttpSession session){
        System.out.println(mf);
        System.out.println("文件类型:"+mf.getContentType());
        System.out.println("表单里面的name属性值:"+mf.getName());
        System.out.println("文件名:"+mf.getOriginalFilename());
        System.out.println("文件大小:"+mf.getSize());
        //System.out.println(""+mf.getInputStream());
        
        //1,得到tomcat里面的upload目录
        String realPath=session.getServletContext().getRealPath("/upload/");
        //2,构造文件
        File file=new File(realPath,mf.getOriginalFilename());
        //3,上传
        try {
            mf.transferTo(file);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        return "../success.jsp";
    }
    
    /**
     * 文件上传2  改名字
     */
    @RequestMapping("upload02")
    public String upload02(MultipartFile mf,HttpSession session){
        String oldName=mf.getOriginalFilename();
        //1,得到tomcat里面的upload目录
        String realPath=session.getServletContext().getRealPath("/upload/");
        String newName=RandomStrUtils.createFileNameUseTime(oldName);
        //2,构造文件
        File file=new File(realPath,newName);
        //3,上传
        try {
            mf.transferTo(file);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        return "../success.jsp";
    }
    /**
     * 文件上传3  改名字  分文件夹管理
     */
    @RequestMapping("upload03")
    public String upload03(MultipartFile mf,HttpSession session){
        //1,得到老名字
        String oldName=mf.getOriginalFilename();
        //2,得到tomcat里面的upload目录
        String realPath=session.getServletContext().getRealPath("/upload/");
        //3,得到当前时间2020-05-03
        String currentDate=RandomStrUtils.getCurrentDateToStr();
        //4,得到新的父目录的路径  并判断是否存在   如果不存在就创建
        String newRealPath=realPath+"/"+currentDate;
        File parentFile=new File(newRealPath);
        if(!parentFile.exists()){
            parentFile.mkdirs();//创建文件夹
        }
        //5,得到新名字
        String newName=RandomStrUtils.createFileNameUseTime(oldName);
        //6,构造文件
        File file=new File(parentFile,newName);
        //3,上传
        try {
            mf.transferTo(file);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        return "../success.jsp";
    }
}

 

文件上传

标签:frame   NPU   random   mapping   ring   nal   文件夹   current   div   

原文地址:https://www.cnblogs.com/97guoxiang/p/12823534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!