时间:2021-05-26
Servlet实现文件上传的三种方法总结
1. 通过getInputStream()取得上传文件。
2. 通过getPart()、getParts()取得上传文件。
body格式:
POST http:// HTTP/1.1 Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA ------WebKitFormBoundaryrGKCBY7qhFd3TrwA Content-Disposition: form-data; name="text" title ------WebKitFormBoundaryrGKCBY7qhFd3TrwA Content-Disposition: form-data; name="file"; filename="chrome.png" Content-Type: image/png PNG ... content of chrome.png ... ------WebKitFormBoundaryrGKCBY7qhFd3TrwA-- [html] view plain copy/** * To change this template, choose Tools | Templates * and open the template in the editor. */ package net.individuals.web.servlet; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; 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; /** * * @author Barudisshu */ @MultipartConfig @WebServlet(name = "UploadServlet", urlPatterns = {"/UploadServlet"}) public class UploadServlet extends HttpServlet { /** * Processes requests for both HTTP * <code>GET</code> and * <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Part part = request.getPart("photo"); String fileName = getFileName(part); writeTo(fileName, part); } //取得上传文件名 private String getFileName(Part part) { String header = part.getHeader("Content-Disposition"); String fileName = header.substring(header.indexOf("filename=\"") + 10, header.lastIndexOf("\"")); return fileName; } //存储文件 private void writeTo(String fileName, Part part) throws IOException, FileNotFoundException { InputStream in = part.getInputStream(); OutputStream out = new FileOutputStream("e:/workspace/" + fileName); byte[] buffer = new byte[1024]; int length = -1; while ((length = in.read(buffer)) != -1) { out.write(buffer, 0, length); } in.close(); out.close(); } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP * <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP * <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; } }3、另一种较为简单的方法:采用part的wirte(String fileName)上传,浏览器将产生临时TMP文件
/** * To change this template, choose Tools | Templates * and open the template in the editor. */ package net.individuals.web.servlet; import java.io.IOException; import java.io.PrintWriter; 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; /** *采用part的wirte(String fileName)上传,浏览器将产生临时TMP文件。 * @author Barudisshu */ @MultipartConfig(location = "e:/workspace") @WebServlet(name = "UploadServlet", urlPatterns = {"/UploadServlet"}) public class UploadServlet extends HttpServlet { /** * Processes requests for both HTTP * <code>GET</code> and * <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //处理中文文件名 request.setCharacterEncoding("UTF-8"); Part part = request.getPart("photo"); String fileName = getFileName(part); //将文件写入location指定的目录 part.write(fileName); } private String getFileName(Part part) { String header = part.getHeader("Content-Disposition"); String fileName = header.substring(header.indexOf("filename=\"") + 10, header.lastIndexOf("\"")); return fileName; } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP * <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP * <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }以上就是Servlet实现文件上传的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
实现Servlet程序的三种方法1.手动实现Servlet程序1.编写一个类去实现Servlet接口2.实现service方法,处理请求并响应数据示例代码://
读取Java文件到byte数组的三种方法(总结)packagezs;importjava.io.BufferedInputStream;importjava.i
java实现多线程的三种方法在java中,有三种方法可以实现多线程。第一种方法:继承Thread类,重写run函数。第二种方法:实现Runnable接口,重写r
本文实例讲述了php实现将数组或对象写入到文件的方法。分享给大家供大家参考,具体如下:php将数组或对象原样写入或保存到文件有三种方法可以实现第一种方法是使用s
python记录程序运行时间的三种方法这里提供了python记录程序运行时间的三种方法,并附有实现代码,最后进行比较,大家参考下:方法1importdateti