首先是表單的jsp文件:upload.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>文件上傳</title> </head> <body> <h1>文件上傳</h1> <form action="message.jsp" method="post" enctype="multipart/form-data"> 選擇一個文件: <input type="file" name="uploadFile"/> <br/><br/> <input type="submit" value="上傳"/> </form> </body> </html>
上傳處理的jsp文件:message.jsp:
<%-- Created by IntelliJ IDEA. User: DELL Date: 2018/4/20 Time: 14:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.io.*,java.util.*,javax.servlet.*,javax.servlet.http.*" %> <%@ page import="java.rmi.ServerException" %> <html> <head> <title></title> </head> <body> <% //定義上傳文件的最大字節 int MAX_SIZE = 102400 * 102400; //創建根路徑的保存變量 String rootPath; //聲明文件讀入類 DataInputStream in = null; FileOutputStream fileOut = null; //取得互聯網程序的絕對地址 String realPath = request.getSession().getServletContext().getRealPath("/"); realPath = realPath.substring(0, realPath.indexOf("\\out")); // out.print(realPath); //創建文件的保存目錄 rootPath = realPath + "\\web\\upload\\"; //取得客戶端上傳的數據類型 String contentType = request.getContentType(); try { if (contentType.indexOf("multipart/form-data") >= 0) { //讀入上傳數據 in = new DataInputStream(request.getInputStream()); int formDataLength = request.getContentLength(); if (formDataLength > MAX_SIZE) { out.print("上傳的字節不可以超過" + MAX_SIZE + "字節"); return; } //保存上傳文件的數據 byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; //上傳的數據保存在byte數組里面 while (totalBytesRead < formDataLength) { byteRead = in.read(dataBytes, totalBytesRead, formDataLength); totalBytesRead += byteRead; } //根據byte數組創建字符串 String file = new String(dataBytes, "utf-8"); //取得上傳數據的文件名 String saveFile = file.substring(file.indexOf("filename=\"") + 10); saveFile = saveFile.substring(0, saveFile.indexOf("\n")); saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\"")); int lastIndex = contentType.lastIndexOf("="); //取得數據的分隔字符串 String boundary = contentType.substring(lastIndex + 1, contentType.length()); //創建保存路徑的文件名 String fileName = rootPath + saveFile; int pos; pos = file.indexOf("filename = \""); pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; pos = file.indexOf("\n", pos) + 1; int boundaryLocation = file.indexOf(boundary, pos) - 4; //取得文件數據的開始的位置 int startPos = ((file.substring(0, pos)).getBytes()).length; int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; File checkFile = new File(fileName); if (checkFile.exists()) { out.println("<p>" + saveFile + "文件已經存在.</p>"); return; } //檢查上傳文件的目錄是否存在 File fileDir = new File(rootPath); if (!fileDir.exists()) { fileDir.mkdirs(); } //創建文件的輸出類 fileOut = new FileOutputStream(fileName); //保存文件的數據 fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.close(); out.print("<b>文件上傳成功</b>"); } else { String content = request.getContentType(); out.print("上傳的文件類型是" + content + "類型的,請上傳目錄mutipart/form-data類型的文件"); } } catch (Exception ex) { throw new ServerException(ex.getMessage()); } %> </body> </html>