您的位置:首页 > Web前端 > JavaScript

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

2014-12-25 00:37 639 查看
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

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: