1.1.  jsp文件上传

1.1.1.  新建一个web工程

1.1.2.  上传页面index.jsp


<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>文件上传</title>
</head>
<body>
         <h3>文件上传</h3>
         <form action="FileUploadServlet" method="post" enctype="multipart/form-data">
                   文件<input type="file" name="file1">
                   <input type="submit" value="提交">
         </form>
</body>
</html>




1.1.3.  文件上传处理FileUploadServlet.java


package com.jsp.file.upload.servlet;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@WebServlet("/FileUploadServlet")
public class FileUploadServlet extends HttpServlet {
         private static final long serialVersionUID = 1L;
 
         public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                  
                   InputStream inputStream = request.getInputStream();
                  
                   String realPath = request.getServletContext().getRealPath("upload");
                  
                   File tempFile = new File(realPath,"temp.tmp");
                  
                   FileOutputStream fos = new FileOutputStream(tempFile);
                  
                   byte[] buffer = new byte[1024];
                  
                   int len = 0;
                  
                   while(-1 != (len = inputStream.read(buffer))){
                            fos.write(buffer, 0, len);
                   }
                  
                   RandomAccessFile randomFile = new RandomAccessFile(tempFile, "r");
                  
                   randomFile.readLine();
                  
                   String contentDisposition = randomFile.readLine();
                  
                   String filename = contentDisposition.substring(contentDisposition.indexOf("filename=\""), contentDisposition.lastIndexOf("\""));
                   filename = filename.replace("filename=\"", "");
                  
                   // 防止中文乱码
                   filename = new String(filename.getBytes("ISO-8859-1"),"UTF-8");
                                    
                   System.out.println(filename);
                  
                  randomFile.seek(0);
                  
                   long start = 0;
                  
                   int forth = 1;
                  
                   while(-1 != (len = randomFile.readByte()) &&  (forth<=4)){
                            if(len == '\n'){
                                     start = randomFile.getFilePointer();
                                     forth++;
                            }
                   }
                  
                   fos.close();
                   inputStream.close();
                  
                   File saveFile = new File(realPath,filename);
                  
                   RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile, "rw");
                  
                   randomFile.seek(randomFile.length());
                  
                   long endPosition = randomFile.getFilePointer();
                  
                   int j = 1;
                  
                   while((endPosition >= 0) && j <= 2){
                            endPosition --;
                            randomFile.seek(endPosition);
                            if(randomFile.readByte() =='\n'){
                                     j++;
                            }
                   }
                  
                   randomFile.seek(start);
                  
                   long startPoint = randomFile.getFilePointer();
                  
                   while(startPoint < endPosition-1){
                            randomAccessFile.write(randomFile.readByte());
                            startPoint = randomFile.getFilePointer();
                   }
                  
                   randomAccessFile.close();
                   randomFile.close();
                   tempFile.delete();
                  
                   System.out.println("文件上传成功");
         }
 
}




1.1.4.  文件上传实例

这个上面代码中获得的临时文件内容的举例,

------WebKitFormBoundarydUEEAVH7AOoNF8Sx

Content-Disposition: form-data; name="file1"; filename="新建文本文档.txt"

Content-Type: text/plain

 

aa

bb

cc

------WebKitFormBoundarydUEEAVH7AOoNF8Sx--