springMVC上传文件

来源:互联网 发布:acfun. 的域名 编辑:程序博客网 时间:2024/04/29 06:57

1.jsp页面


<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
  <title>文件上传</title>
</head>


<body>
<form action="${pageContext.request.contextPath}/upload/file" enctype="multipart/form-data" method="post">
  上传用户:<input type="text" name="username"><br/>
  上传文件1:<input type="file" name="attachs"><br/>
  上传文件2:<input type="file" name="attachs"><br/>
  <input type="submit" value="提交">
</form>
</body>
</html>


2.controller接收和保存文件

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


/**
 * 文件上传控制器
 * Created by Administrator on 2016-11-29.
 */
@Controller
@RequestMapping("/upload")
public class FileUploadController {


    @RequestMapping(value = "file", method = RequestMethod.POST)
    @ResponseBody
    public String upload(HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        String username = request.getParameter("username");
        System.out.println(username);
        //文件数组
        List<MultipartFile> files = multipartRequest.getFiles("attachs");
        //单个文件
//        MultipartFile multipartFile = multipartRequest.getFile("attachs");
        if(!files.isEmpty()){
            for(MultipartFile file : files){
                String path = setFilePath();
                //获取上传文件的名字
                String filename = file.getOriginalFilename();
                //获取上传文件的类型 如:.png,.xml,.java等
                filename = filename.substring(filename.lastIndexOf("."), filename.length());
                //使用UUID获取新保存文件名称,防止重复
                path = path + File.separator + UUID.randomUUID().toString() + filename;
                FileOutputStream output = new FileOutputStream(new File(path));
                byte[] bytes = file.getBytes();
                output.write(bytes, 0, bytes.length);
                output.close();
            }
        }
        return "file upload success";
    }


    /**
     * 设置上传文件保存路径
     * @return
     */
    private String setFilePath(){
        String path=Thread.currentThread().getContextClassLoader().getResource("").toString();
        path=path.replace('/', '\\'); // 将/换成\
        path=path.replace("file:", ""); //去掉file:
        path=path.replace("classes\\", ""); //去掉class\
        path=path.replace("target\\", ""); //去掉class\
        path=path.substring(1); //去掉第一个\,如 \D:\JavaWeb...
        path+= "images";
        File file = new File(path);
        //目录或文件不存在就进行创建
        if(!file.exists()){
            file.mkdirs();
        }
        return path;
    }
}


0 0
原创粉丝点击