JSP&&SERVLET学习笔记(七):Servlet处理上传的文件

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;
import javax.swing.text.Position;


@WebServlet("/upload.do")
public class UploadServlet extends HttpServlet {


    private byte[] readBody(HttpServletRequest request) throws IOException{
    int formDataLength = request.getContentLength();
   
    //取得ServletInputStream对象
    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;
   
    }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

//读取请求Body
byte[] body = readBody(request);

//获取所有Body内容的字符串表示
String textBody = new String(body, "ISO-8859-1");

//取得上传的文件名称
String filename = getFilename(textBody);

//取得文件开始与结束的位置
Position p = getFilePosition(request, textBody);

//输出至文件
writeTo(filename, body, p);
}


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);
}


class Position{
int begin;
int end;
Position(int begin, int end){
this.begin = begin;
this.end = 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));
   
   
   
    }
    


   

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}






}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。