Use jsp to realize the function of file upload

The first is the jsp file of the form: upload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>File upload</title>
</head>
<body>
<h1>File upload</h1>

<form action="message.jsp" method="post" enctype="multipart/form-data">
    Choose a file:
    <input type="file" name="uploadFile"/>
    <br/><br/>
    <input type="submit" value="上传"/>
</form>
</body>
</html>

  Upload the processed jsp file: 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>
<%
    //Define the maximum bytes of the uploaded file
    int MAX_SIZE = 102400 * 102400;
    //Create a save variable for the root path
    String rootPath;
    //declaration file read into class
    DataInputStream in = null;
    FileOutputStream fileOut = null;
    //Get the absolute address of the Internet program
    String realPath = request.getSession().getServletContext().getRealPath("/");
    realPath = realPath.substring(0, realPath.indexOf("\\out"));
//    out.print(realPath);
    //Create a directory to save the file
    rootPath = realPath + "\\web\\upload\\";
    //Get the data type uploaded by the client
    String contentType = request.getContentType();
    try {
        if (contentType.indexOf("multipart/form-data") >= 0) {
            //read upload data
            in = new DataInputStream(request.getInputStream());
            int formDataLength = request.getContentLength();
            if (formDataLength > MAX_SIZE) {
                out.print("Uploaded bytes cannot exceed" + MAX_SIZE + "bytes");
                return;
            }
            // save the data of the uploaded file
            byte dataBytes[] = new byte[formDataLength];
            int byteRead = 0;
            int totalBytesRead = 0;
            //The uploaded data is stored in the byte array
            while (totalBytesRead < formDataLength) {
                byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
                totalBytesRead + = byteRead;
            }
            //create string from byte array
            String file = new String(dataBytes, "utf-8");
            //Get the filename of the uploaded data
            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("=");
            //get the delimited string of data
            String boundary = contentType.substring(lastIndex + 1, contentType.length());
            //Create a filename for the save path
            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;
            //Get the starting position of the file data
            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 + "The file already exists.</p>");
                return;
            }
            //Check if the directory where the uploaded file exists
            File fileDir = new File(rootPath);
            if (!fileDir.exists()) {
                fileDir.mkdirs();
            }
            //create the output class of the file
            fileOut = new FileOutputStream(fileName);
            // save the data of the file
            fileOut.write(dataBytes, startPos, (endPos - startPos));
            fileOut.close();
            out.print("<b>File uploaded successfully</b>");
        } else {
            String content = request.getContentType();
            out.print("The uploaded file is of type " + content + ", please upload a file of type mutipart/form-data");
        }
    } catch (Exception ex) {
        throw new ServerException(ex.getMessage());
    }
%>
</body>
</html>

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324725867&siteId=291194637