通过Servlet实现页面上传文件(到硬盘F)

原创 2016年08月31日 10:37:41
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;
		}

	}
}

版权声明:本博客是博主在学习中遇到的问题,欢迎查看。 举报

相关文章推荐

将上传文件保存到指定目录的一个java源代码

package com.itcast.util; import java.io.File; import java.io.FileNotFoundException; import java...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)