通過Servlet實現頁面上傳文件(到硬盤F)

package servlet4;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileUpLoad extends HttpServlet {

	// 編碼
	private String encoding = "UTF-8";
	// 解碼
	private String decoding = null;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");

		ServletInputStream sis = req.getInputStream();

		int len = req.getContentLength();
		int index = 0;
		String tmp = null;
		boolean isFirst = true;
		String firstLine = null;
		int[] iindex = new int[1];
		byte[] bytes = new byte[4096];
		String filename = null;

		while ((tmp = readLine(bytes, iindex, sis, encoding)) != null) {
			if (isFirst) {
				firstLine = tmp;
				isFirst = false;
			}
			index = tmp.indexOf("filename=");

			// 得到要上傳文件的文件名
			if (index != -1) {
				String tailString = tmp.substring(index + 10);
				if (tailString != null) {
					int ii = tailString.indexOf("\"");
					filename = tailString.substring(0, ii);
				}
				System.out.println(tmp);
				break;
			}
		}
		filename = getName(filename);
		if (filename == null) {
			filename = "file.out1";
		}
		String filepath = "f:/" + filename;
		FileOutputStream fos = new FileOutputStream(filepath);

		// 定義文件上傳結束標誌
		String endFlag = firstLine.substring(0, firstLine.length() - 2) + "--"
				+ firstLine.substring(firstLine.length() - 2);
		String contentType = readLine(bytes, iindex, sis, encoding);
		if (contentType != null) {
			if (contentType.indexOf("content-type") == -1) {
				System.out.println(contentType);
			} else {
				System.out.println("the head of file"
						+ readLine(bytes, iindex, sis, encoding));
			}
		}
		boolean tt = false;
		int mark = 0;

		byte[] backups = new byte[4096];
		while ((tmp = readLine(bytes, iindex, sis, encoding)) != null) {
			if (endFlag.equals(tmp)) {
				if (mark > 2) {
					fos.write(backups, 0, mark - 2);
					fos.flush();
				}
				break;
			} else {
				if (tt) {
					fos.write(backups, 0, mark);
					fos.flush();
				}
				mark = iindex[0];
				for (int i = 0; i < iindex[0]; i++) {
					backups[i] = bytes[i];
				}
				tt = true;
			}
		}
		fos.close();
		sis.close();
	}

	// 獲取上傳的文件名
	protected String getName(String name) {
		String rtn = null;
		if (name != null) {
			int index = name.lastIndexOf("/");
			if (index != -1) {
				rtn = name.substring(index + 1);
			} else {
				index = name.lastIndexOf("\\");
				if (index != -1) {
					rtn = name.substring(index + 1);
				} else {
					rtn = name;
				}
			}
		}
		return rtn;
	}

	// 讀取每一行
	protected String readLine(byte[] bytes, int[] index,
			ServletInputStream sis, String encoding) {
		try {
			index[0] = sis.readLine(bytes, 0, bytes.length);
			if (index[0] < 0)
				return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		try {
			if (encoding == null) {
				return new String(bytes, 0, index[0]);
			} else {
				return new String(bytes, 0, index[0], encoding);
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}
}

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