Upload simple template based download written 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>

Download interface

The main method of file downloads

 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 {
        the response.setContentType ( "text / HTML; charset = UTF-. 8" );
         // Set the response file for download stream
         // response.setHeader ( "the Content-Disposition", "Attachment; filename" + fileName); 
        Response. setHeader ( "Content-Disposition", "Attachment; filename =" + fileName);
         // ServletOutputStream outputStream = response.getOutputStream (); 
        ServletOutputStream outputStream = response.getOutputStream ();
         // get the full path (absolute path) file
         // . realpath = request.getServletContext String () the getRealPath ( "Files"); 
        . = request.getServletContext realpath String () the 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();
    }
}

Control files downloaded Controller

<%--
  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")) { // random ID is provided to prevent the same name as the file String UUID = UUID.randomUUID () toString ().; FileUtils.copyInputStreamToFile(file.getInputStream(),new File("E:/"+uuid+suffix)); return "success"; } return "error"; }
}

File Upload

1 based on the apache file upload completion commons-fileupload.jar
2 MultipartResovler role
2.1 client upload a file transfer package type into MultipartFile
2.2 MultipartFile package acquired by the class file stream
3 form data type classification
3.1 enctype attribute control table form the type of "multipart / form-data"

4 MultipartFile controller parameter name must be consistent with the input file name tag attribute values

5 core file upload method

 FileUtils.copyInputStreamToFile(InputStream,File);

Guess you like

Origin www.cnblogs.com/qyx66/p/12109708.html