文章目录
servlet文件下载
import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; 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.FileInputStream; import java.io.IOException; import java.io.InputStream; @WebServlet("/downloadServlet") public class download extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.获取需要下载的文件名 String filename = req.getParameter("filename"); //2.得到下载的文件存放路径 String filepath = req.getServletContext().getRealPath("E:/java1/servlet04/src/main/webapp/upload"); //3.通过路径与文件名得到file对象 File file = new File(filename+filepath); //4.判断file对象是否存在,且是一个标准文件 if (file.exists() && file.isFile()){ //5.设置相应类型 resp.setContentType("application/x-msdomload"); //6.设置响应头(下载文件名等) resp.setHeader("Contest-Disposition","attachment;filename="+filename); //7.得到要下载的文件的输入流 InputStream in = new FileInputStream(file); //8.得到字节输出流 ServletOutputStream out =resp.getOutputStream(); //9.循环输出 int len = 0; byte[]bytes = new byte[1024]; while ((len = in.read(bytes))!=-1){ out.write(bytes,0,len); } //10.关闭流 out.close(); in.close(); }else { resp.setContentType("text/html;charset=UTF-8"); resp.getWriter().write("<h2>要下载的文件不存在!!</h2>"); resp.getWriter().close(); } } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>文件下载</title> </head> <body> <!-- 超链接下载--> <a href="../../../target/servlet04-1.0-SNAPSHOT/uploadQQ图片20210315175557.jpg" download="li">图片</a> <form action="downloadServlet"> 文件名:<input type="text" name="filename"> <button>下载</button> </form> </body> </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; /** * 文件上传 * 使用注解 @MultipartConfig 将一个Servlet 标识为支持文件上传 * Servlet 将 multipart/form-data 的POST请求封装成 Part ,通过Part 对上传文件进行操作。 * Part part = request.getPart(name) * name代表的是表单元素(文件域)的name属性值 */ @MultipartConfig @WebServlet("/uploadServlet") public class uploadServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置请求的编码格式 req.setCharacterEncoding("UTF-8"); //获取普通文本框 String uname = req.getParameter("uname"); System.out.println("用户名:"+uname); //得到part对象 Part part = req.getPart("myfile"); //得到上传文件的文件名 String filename = part.getSubmittedFileName(); //设置上传的文件要存放的路径 String uploadPath = req.getServletContext().getRealPath("E:/java1/servlet04/src/main/webapp/upload"); //上传文件 part.write(uploadPath + filename); } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>文件上传</title> </head> <body> <!-- 文件上传表单 (二进制表单) 1.表单类型 普通表单:enctype=“application/x-www-form-urlencoded” (默认) 二进制表单:enctype=“multipart/form-data” (文件上传表单) 2.提交方式: GET请求 与 POST请求 文件上传需要设置为 method="post" 3.表单元素: 文件域 <input type="file" name="myname"/> 需要设置表单元素的name属性值 4.提交路径 处理上传文件的Servlet的对外访问路径 --> <form action="uploadServlet" enctype="multipart/form-data" method="post"> 姓名:<input type="text" name="uname"><br> 头像:<input type="file" name="myfile"> <button>提交</button> </form> </body> </html>