使用servlet新特性,Part对象实现文件上传,文件下载,简单

文件上传:

upload.html




    
    文件上传



    
    

出来上传请求的servlet

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

/**
 * @version 1.0
 * @author: liujiahuai
 * @date: 2019/8/22 0022 17:08
 */
@WebServlet(name = "UploadServlet",value = "/uploadServlet")
@MultipartConfig
public class UploadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
        // 创建part对象
        Part part = request.getPart("fileName");
        String contentDisposition = part.getHeader("Content-Disposition");

        // 动态获取服务器upload文件夹的路径
        String serverPath = request.getServletContext().getRealPath("download");

        if (part.getContentType().contains("image")){
            String suffix = contentDisposition.substring(contentDisposition.lastIndexOf("=")+2,contentDisposition.length()-1);
            // 随机的生成一个32的字符串
            String fileName = UUID.randomUUID()+suffix;
            // 向指定的文件夹写入文件
            part.write(serverPath+"\\"+fileName);
            writer.print("上传文件成功!!");
            writer.print("返回");
        }else {
            writer.print("请选择正确的图片格式!!!");
        }
        writer.flush();
        writer.close();
    }

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

 

文件下载:

download.html




    
    servlet方式下载



    

通过servlet程序下载文件

压缩包 图片

出来下载请求的servlet:

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.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @version 1.0
 * @author: liujiahuai
 * @date: 2019/8/26 0026 10:56
 */
@WebServlet(name = "DownLoadServlet",value = "/downLoadServlet")
public class DownLoadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 获得请求文件名
        String fileName = request.getParameter("fileName");

        // 设置文件MIME类型
        String mimeType = getServletContext().getMimeType(fileName);
        response.setContentType(mimeType);

        // 设置Content-Disposition
        response.setHeader("Content-Disposition", "attachment;filename="+fileName);

        // 读取目标文件,通过response将目标文件写到客户端
        // 获取目标文件的绝对路径
        String fullFileName = getServletContext().getRealPath("/download/" + fileName);
        // 创建输入输出流对象
        InputStream in = new FileInputStream(fullFileName);
        OutputStream out = response.getOutputStream();

        // 读写文件
        int b;
        while((b=in.read())!= -1) {
            out.write(b);
        }

        out.close();
        in.close();
    }
}

注:上传需使用@MultipartConfig注解;相关的download、upload文件夹需先创建。

 

你可能感兴趣的:(servlet,文件上传,文件下载,servlet新特性实现,不需外部jar包依赖,servlet)