Java文件上傳:原始Servlet實現

參考轉載:

https://www.cnblogs.com/EasonJim/p/6554669.html

https://my.oschina.net/Barudisshu/blog/150026

原理:

利用getInputStream()讀取整個上傳表單內容,然後通過分析body中的filename字符索引,獲取到文件名稱以及跟在後面的文件內容的起始位置,然後讀取到服務器。

讀取到的內容形式一般如下,使用--boundary分割每個表單項。

代碼:

1、upload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>upload</title>
</head>
<body>
    <p>選擇要上傳的文件</p>
    <form action="accept.jsp" method="post" enctype="multipart/form-data">
        <input type="text" name="girl" value="susan">
        <input type="file" name="boy" size="38">
        <br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

2、accept.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>接收文件</title>
</head>
<body>
    <%!
        class Position {
            int begin;
            int end;

            public Position(int begin, int end) {
                this.begin = begin;
                this.end = end;
            }
        }
    %>
    <%
        //        try {
//            InputStream in = request.getInputStream();
//            File dir = new File(application.getRealPath("/"),"upload");
//            dir.mkdir();
//            File f=new File(dir,"b.txt");
//            FileOutputStream outstream=new FileOutputStream(f);
//            byte[] b= new byte[100];
//            int n=0;
//            while ((n = in.read(b)) != -1) {
//                outstream.write(b,0,n);
//            }
//            in.close();outstream.close();
//            out.print("文件已上傳");
//        } catch (IOException e) {
//            e.printStackTrace();
//            out.print("上傳失敗");
//        }
        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;
        }

        String textBody = new String(body, "ISO-8859-1");
        String fileName = textBody.substring(textBody.indexOf("filename=\"") + 10);//獲取文件名
        fileName = fileName.substring(0, fileName.indexOf("\n"));
        fileName = fileName.substring(fileName.indexOf("\n") + 1, fileName.indexOf("\""));
        fileName= new String( fileName.getBytes("ISO-8859-1"),"UTF-8");

        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;
        Position p = new Position(begin, end);

        FileOutputStream fileOutputStream = new FileOutputStream(request.getServletContext().getRealPath("/" + fileName));
        fileOutputStream.write(body, p.begin, (p.end - p.begin));
        fileOutputStream.flush();
        fileOutputStream.close();


    %>
</body>
</html>

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章