Servlet中使用getInputStream进行文件上传
生活随笔
收集整理的這篇文章主要介紹了
Servlet中使用getInputStream进行文件上传
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
據說古老了點,所以代碼比較繁瑣,主要用于處理文件的地方太多。
下節用SERVLET3.0的Part進行操作一下。
form.html:
<!DOCTYPE html> <html> <head><meta http-equiv="content-type" content="text/html ;charset=UTF-8"> </head> <body><form method="post" action="upload.do" enctype="multipart/form-data">file: <input type="file" name="filename" value="" /><br><input type="submit" value="Upload" name="upload" /></form> </body> </html>uploadServlet.java:
package cc.openhome;import java.io.DataInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** Servlet implementation class UploadServlet*/ @WebServlet("/upload.do") public class UploadServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public UploadServlet() {super();// TODO Auto-generated constructor stub }/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubbyte[] body = readBody(request);String textBody = new String(body, "ISO-8859-1");String filename = getFilename(textBody);Position p = getFilePosition(request, textBody);writeTo(filename, body, p);}class Position {int begin;int end;Position(int begin, int end) {this.begin = begin;this.end = end;}}private byte[] readBody(HttpServletRequest request)throws IOException{int formDataLength = request.getContentLength();DataInputStream dataStream = new DataInputStream(request.getInputStream());byte body[] = new byte[formDataLength];int totalBytes = 0;while (totalBytes < formDataLength) {int bytes = dataStream.read(body, totalBytes, formDataLength);totalBytes += bytes;}return body;}private Position getFilePosition(HttpServletRequest request, String textBody) throws IOException {String contentType = request.getContentType();String boundaryText = contentType.substring(contentType.lastIndexOf("=") + 1, contentType.length());int pos = textBody.indexOf("filename=\"");pos = textBody.indexOf("\n", pos) + 1;pos = textBody.indexOf("\n", pos) + 1;pos = textBody.indexOf("\n", pos) + 1;int boundaryLoc = textBody.indexOf(boundaryText, pos) -4;int begin = ((textBody.substring(0, pos)).getBytes("ISO-8859-1")).length;int end = ((textBody.substring(0, boundaryLoc)).getBytes("ISO-8859-1")).length;return new Position(begin, end);}private String getFilename(String reqBody) {String filename = reqBody.substring(reqBody.indexOf("filename=\"") + 10);filename = filename.substring(0, filename.indexOf("\n"));filename = filename.substring(filename.lastIndexOf("\\") + 1, filename.indexOf("\""));return filename;}private void writeTo(String filename, byte[] body, Position p)throws FileNotFoundException, IOException {FileOutputStream fileOutputStream =new FileOutputStream("c:/workspace/" + filename);fileOutputStream.write(body, p.begin, (p.end - p.begin));fileOutputStream.flush();fileOutputStream.close();}}?
轉載于:https://www.cnblogs.com/aguncn/p/5468066.html
總結
以上是生活随笔為你收集整理的Servlet中使用getInputStream进行文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【计算机视觉】森林火灾检测-1
- 下一篇: batch 批处理获取系统时间