package com.uploadify;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

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 UploadifySerlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	//上传文件的保存路径
	protected String configPath = "upload/";

	protected String dirTemp = "upload/temp/";
	
	protected String dirName = "file";
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 this.doPost(request, response);
	}

	 
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		//文件保存目录路径
		String savePath = this.getServletContext().getRealPath("/") + configPath;
		
		// 临时文件目录 
		String tempPath = this.getServletContext().getRealPath("/") + dirTemp;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String ymd = sdf.format(new Date());
		savePath += "/" + ymd + "/";
		//创建文件夹
		File dirFile = new File(savePath);
		if (!dirFile.exists()) {
			dirFile.mkdirs();
		}
		
		tempPath += "/" + ymd + "/";
		//创建临时文件夹
		File dirTempFile = new File(tempPath);
		if (!dirTempFile.exists()) {
			dirTempFile.mkdirs();
		}
		
		DiskFileItemFactory  factory = new DiskFileItemFactory();
		//设定使用内存超过20M时,将产生临时文件并存储于临时目录中。   
		factory.setSizeThreshold(20 * 1024 * 1024); 
		//设定存储临时文件的目录。   
		factory.setRepository(new File(tempPath)); 

		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("ISO8859_1");
		 
		try {
			List items = upload.parseRequest(request);
			Iterator itr = items.iterator();
			
			String name = "";
			String qq = "";
			
			while (itr.hasNext()) {
				FileItem item = (FileItem) itr.next();
				String fileName = item.getName();
				if(fileName!=null){
					fileName = new String(fileName.getBytes("ISO8859_1"),"utf-8");  
				}
				
				long fileSize = item.getSize();
				if (!item.isFormField()) {
					String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
					
					SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
					String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
					try{
						File uploadedFile = new File(savePath, newFileName);
	                    OutputStream os = new FileOutputStream(uploadedFile);
	                    InputStream is = item.getInputStream();
	                    //可以修改 1024 以提高读取速度
	                    byte buf[] = new byte[1024];
	                    int length = 0;  
	                    while( (length = is.read(buf)) > 0 ){  
	                        os.write(buf, 0, length);  
	                    }  
	                    //关闭流  
	                    os.flush();
	                    os.close();  
	                    is.close();  
	                    System.out.println("上传成功!路径:"+savePath+"/"+newFileName);
	                    out.print("1");
					}catch(Exception e){
						e.printStackTrace();
					}
				}else {
					String filedName = item.getFieldName();
					if (filedName.equals("userName")) {
						name = item.getString();
						name = new String(name.getBytes("ISO8859_1"),"utf-8");  
					}else {
						qq = item.getString();
						qq = new String(qq.getBytes("ISO8859_1"),"utf-8"); 
					}
					System.out.println("FieldName:"+filedName);
					System.out.println("String:"+new String(item.getString().getBytes("ISO8859_1"),"utf-8"));
					System.out.println("==============="); 
				}			
			} 
			
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		out.flush();
		out.close();
	}

}
最近下载更多
完美的会飞翔的胖子  LV1 2022年4月8日
oldfox  LV18 2022年2月22日
13720359621  LV2 2021年12月7日
czr2233  LV9 2021年9月14日
最代码-宋家辉  LV61 2021年5月16日
zhanqiuxiang0202  LV1 2021年2月2日
丶右转  LV11 2020年12月1日
zhanss  LV7 2020年8月15日
yqd123456  LV3 2020年6月8日
zd5468  LV7 2020年4月26日
最近浏览更多
1358849392  LV21 4月12日
yymmdm  LV6 2022年9月5日
xuexizhuanyong23  LV16 2022年8月7日
sl0018  LV13 2022年6月24日
陈小灏  LV14 2022年6月1日
完美的会飞翔的胖子  LV1 2022年4月8日
oldfox  LV18 2022年2月22日
17798830  LV14 2022年1月25日
13720359621  LV2 2021年12月7日
1006640152  LV11 2021年11月25日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友