上传
前端

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                            "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
     <form action="/UploadServlet" method="post"
      enctype="multipart/form-data">
         <table width="600px">
             <tr>
                 <td>上传者</td>
                 <td><input type="text" name="username" /></td>
             </tr>
             <tr>
                 <td>上传文件</td>
                 <td><input type="file" name="myfile" /></td>
             </tr>
             <tr>
                 <td colspan="2"><input type="submit" value="上传" /></td>
             </tr>
         </table>
     </form>
</body>
</html>

后端

package com.example.javaweb17;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

public class UploadServlet extends HttpServlet {
    private static final long serialVersionUID =1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=utf-8");
        try {
            DiskFileItemFactory factory=new DiskFileItemFactory();
            File f = new File("/Users/chenci/download/test.txt");
            if (!f.exists()){
                f.mkdir();
            }
            factory.setRepository(f);
            factory.setSizeThreshold(1024*1024);

            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            fileUpload.setHeaderEncoding("utf-8");
            List<FileItem> fileItems=fileUpload.parseRequest(request);

            for (FileItem fileItem:fileItems){
                boolean flag = fileItem.isFormField();
                if (flag){
                    String name = fileItem.getFieldName();
                    if (name.equals("username")){
                        String value = fileItem.getString("uft-8");
                        response.getWriter().print("从网页上传者:"+value+"</br>");
                    }
                }else{
                    String filename = fileItem.getName();
                    if (filename!=null&&!filename.equals("")){
                        response.getWriter().print("从网页上文件名称:"+filename+"</br>");
                        filename=filename.substring(filename.lastIndexOf("\\")+1);
                        filename= UUID.randomUUID().toString()+"_"+filename;
                        String webPath = "/upload/";
                        String filiepath = getServletContext().getRealPath(webPath+filename);
                        File file = new File(filiepath);
                        file.getParentFile().mkdir();
                        file.createNewFile();

                        InputStream in = fileItem.getInputStream();
                        FileOutputStream out = new FileOutputStream(file);

                        byte[] buffer = new byte[1024];
                        int len=0;
                        while ((len=in.read(buffer))>0){
                            out.write(buffer,0,len);
                        }
                        in.close();
                        out.close();
                        fileItem.delete();
                        response.getWriter().print("文件上传成功"+"</br>");
                    }
                }
            }
        }catch (FileUploadException e){
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        doGet(request,response);
    }
}

下载
前端

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.net.URLEncoder"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                            "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件下载</title>
</head>
<body>
<a href="/DownloadServlet?filename=img.png">文件下载-不是中文</a>
<br/><br/><br/>
 <a href="/DownloadServlet?filename=<%=URLEncoder.encode("风景.jpg","utf-8")%>">文件下载 -是中文</a>
<br/><br/><br/>
<a href="DownloadServlet?filename=风景.jpg">文件下载-中文之第二种解决方法</a>
<br/><br/><br/>
</body>
</html>

后端

package com.example.javaweb17;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;


public class DownloadServlet extends HttpServlet {
    private static final long serialVersionUID=1L;

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String filename = req.getParameter("filename");
        filename= new String(filename.getBytes("iso-8859-1"),"utf-8");
        String fileType = getServletContext().getMimeType(filename);
        resp.addHeader("Content-type",fileType);

        String folder = "/download/";
        InputStream in = getServletContext().getResourceAsStream(folder+filename);
        OutputStream out = resp.getOutputStream();

        byte[] buffer = new byte[1024];
        int len = 0;
        while((len=in.read(buffer))!=-1){
            out.write(buffer,0,len);
        }
        out.close();
        in.close();
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

测试
F10E4405D17A777B4A9776643C57F9C5.jpg

552134650187EDECA83C337C6541829D.jpg

3371C76C5360B1231BB4C788C5514C78.jpg