upfile.java 4.5 KB
package com.servlet.bksh.DMC;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

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

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class upfile extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2325826340972403064L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		String type = null;
		DiskFileItemFactory difactory = new DiskFileItemFactory();
		// 创建文件处理工厂,它用于生成FileItem对象。

		difactory.setSizeThreshold(1024 * 1024);
		// 设置处理工厂缓存的临时目录,此目录下的文件需要手动删除。

//		String dir = this.getServletContext().getRealPath("/");
		String dir =  "/usr/bk_cx/";
		//String dir =  "C://";

		File filedir = new File(dir + "emp");

		if (!filedir.exists())

			filedir.mkdir();

		difactory.setRepository(filedir);

		// 设置文件实际保存的目录

		// String userdir = dir + "bksh_info";
		//
		// File fudir = new File(userdir);
		//
		// if (!fudir.exists())
		//
		// fudir.mkdir();

		// 创建request的解析器,它会将数据封装到FileItem对象中。

		ServletFileUpload sfu = new ServletFileUpload(difactory);

		// 解析保存在request中的数据并返回list集合

		List<?> list = null;

		try {

			list = sfu.parseRequest(request);

		} catch (FileUploadException e) {

			e.printStackTrace();

		}

		// 遍历list集合,取出每一个输入项的FileItem对象,并分别获取数据
		for (Iterator<?> it = list.iterator(); it.hasNext();) {

			FileItem fi = (FileItem) it.next();

			if (fi.isFormField()) {
				//
				// System.out.println("type?" + fi.getFieldName());
				//
				// System.out.println("value?" + fi.getString());
				type = fi.getString();
			} else {
				// 由于客户端向服务器发送的文件是客户端的全路径,在这我们只需要文件名即可

				String filename = fi.getName();
				if (!filename.endsWith("jpg") && !filename.endsWith("jpeg")
						&& !filename.endsWith("docx")
						&& !filename.endsWith("doc")
						&& !filename.endsWith("ppt")
						&& !filename.endsWith("pdf")
						&& !filename.endsWith(".xls")
						&& filename.endsWith(".xlsx")) {
					response.getWriter()
							.print("{success:true,msg:'◇上传功能仅支持.jpg/.jpeg/.docx/.doc/.pdf/.xls/.xlsx格式的文件上传◇'}");
					response.getWriter().close();
					return;
				}
				int index = filename.lastIndexOf("\\");

				if (index != -1)

					filename = filename.substring(index + 1);
				
				// 向服务器写出文件

				InputStream in = fi.getInputStream();
				String userdir = null;
				if (type.equals("info"))
					userdir = dir + "bksh_info";
				else if (type.equals("report"))
					userdir = dir + "bksh_report";
				else if (type.equals("scheme"))
					userdir = dir + "bksh_scheme";

				File fudir = new File(userdir);

				if (!fudir.exists())

					fudir.mkdir();

				File fudirpath = new File(fudir + "/" +filename);//"/" + DataBase.encodeFileName(request, filename)
				if (fudirpath.exists()) {
					response.getWriter().print(
							"{success:true,msg:'◇您上传的文件已存在,请删除后再次上传◇'}");
					response.getWriter().close();
					return;
				}
				FileOutputStream fos = new FileOutputStream(fudirpath);

				byte[] buf = new byte[1024];

				int len = -1;

				while ((len = in.read(buf)) != -1) {
					fos.write(buf, 0, len);
				}
				// 关闭流
				if (in != null) {

					try {

						in.close();

					} finally {

						if (fos != null)

							fos.close();
					}
				}
			}
		}
		response.getWriter().print("{success:true,msg:'◇上传成功◇'}");
		response.getWriter().close();
	}
}