根据SpringMVC编写的下载上传简易模板

<%--
  Created by IntelliJ IDEA.
  User: 13554
  Date: 2019/12/23
  Time: 22:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <a href="download?fileName=a.txt">下载</a>
</body>
</html>

下载界面

文件下载的主要方法

 byte[] bytes=FileUtils.readFileToByteArray(file);
        outputStream.write(bytes);
package com.qyx.controller;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class DemoController {
    @RequestMapping("/download")
    public void download(String fileName, HttpServletResponse response, HttpServletRequest request) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        //设置响应流中的文件进行下载
        //response.setHeader("Content-Disposition","attachment;filename"+fileName);
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        //ServletOutputStream outputStream=response.getOutputStream();
        ServletOutputStream outputStream=response.getOutputStream();
        //获取文件完整路径(绝对路径)
        //String realPath=request.getServletContext().getRealPath("files");
        String realPath=request.getServletContext().getRealPath("files");
        //File file=new File(realPath,fileName);
        File file=new File(realPath,fileName);
        //byte[] bytes=FileUtils.readFileToByteArray(file);
        byte[] bytes=FileUtils.readFileToByteArray(file);
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    }
}

控制文件下载的控制器

<%--
  Created by IntelliJ IDEA.
  User: 13554
  Date: 2019/12/25
  Time: 16:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<!--需要设置enctype="multipart/form-data"才能进行文件上传-->
<form method="post" enctype="multipart/form-data" action="upload">
    姓名:<input type="text" name="name">
    文件:<input type="file" name="file">
    <input type="submit" value="提交">
</form>
</body>
</html>
package com.qyx.controller;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class DemoController {
@RequestMapping("/upload")
  
public String upload(MultipartFile file,String name) throws IOException { //String fileName=file.getOriginalFilename(); String fileName=file.getOriginalFilename(); //String suffix=fileName.substring(fileName.lastIndexOf(".")); String suffix=fileName.substring(fileName.lastIndexOf(".")); if (suffix.equalsIgnoreCase(".txt")) { //为防止文件重名而设置的随机ID String uuid= UUID.randomUUID().toString(); FileUtils.copyInputStreamToFile(file.getInputStream(),new File("E:/"+uuid+suffix)); return "success"; } return "error"; }
}

文件上传

1 基于apache的commons-fileupload.jar完成文件上传
2 MultipartResovler作用
2.1 把客户端上传的文件流转换成MultipartFile封装类
2.2 通过MultipartFile封装类获取到文件流
3 表单数据类型分类
3.1 在form的enctype属性控制表单类型为 "multipart/form-data"

4 控制器中MultipartFile的参数名必须和input file标签里的name属性值保持一致

5 文件上传核心方法

 FileUtils.copyInputStreamToFile(InputStream,File);
posted @ 2019-12-27 21:34  计算机的探索者  阅读(221)  评论(0编辑  收藏  举报