www.pudn.com > restrant.zip > ClusterService.java, change:2014-03-04,size:4192b


package com.alex.restrantol.service; 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.List; 
import java.util.UUID; 
 
import javax.servlet.ServletException; 
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; 
 
import com.alex.restrantol.dao.ClusterDao; 
import com.alex.restrantol.entity.Cluster; 
import com.alex.restrantol.entity.UserEntity; 
 
public class ClusterService { 
 
	public boolean addCluster(HttpServletRequest request,HttpServletResponse response){ 
		boolean flag = false; 
		ClusterDao dao = new ClusterDao(); 
		Cluster cluster = new Cluster(); 
		UserEntity user; 
		user = (UserEntity) request.getSession().getAttribute("user"); 
		String category = request.getParameter("category"); 
		String userName = user.getName(); 
		String title = request.getParameter("title"); 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); 
		Date time = null; 
		try { 
			time = sdf.parse(request.getParameter("activityTime")); 
		} catch (ParseException e) { 
			e.printStackTrace(); 
		} 
		String place = request.getParameter("clusterPlace"); 
		String content = request.getParameter("content"); 
		if(category != null && userName != null && title != null && time != null && place != null && content != null){ 
			cluster.setCategory(category); 
			cluster.setUserName(userName); 
			cluster.setTitle(title); 
			cluster.setActivityTime(time); 
			cluster.setClusterPlace(place); 
			cluster.setClusterInfo(content); 
			cluster.setStatus(1); 
			cluster.setRecommended(1); 
			if(dao.addCluster(cluster)) 
			flag = true; 
		} 
		return flag; 
	} 
	 
	public boolean replenishCluster(HttpServletRequest request,HttpServletResponse response, String tempPath, String uploadPath)throws ServletException, IOException{ 
		String category = null; 
		File file; 
		// 创建文件上传工厂 
		DiskFileItemFactory factory = new DiskFileItemFactory(); 
		// 设置内存中缓冲区的大小,默认10K 
		factory.setSizeThreshold(100*1024); 
		// 设置上传文件临时存放的目录 
		factory.setRepository(new File(tempPath)); 
		// 创建上传文件对象[核心] 
		ServletFileUpload upload = new ServletFileUpload(factory); 
		// 客户端上传文件是否使用MIME协议 
		boolean flag = upload.isMultipartContent(request); 
		boolean flag1 = false; 
		if(!flag){ 
			throw new ServletException(); 
		}else{ 
			try { 
				List<FileItem> fileItemList = upload.parseRequest(request); 
				for(FileItem fileItem : fileItemList){ 
					if(fileItem.isFormField()){ 
						if(fileItem.getFieldName().equals("category")) 
							category = fileItem.getString("utf-8"); 
					} 
					if(fileItem.isFormField()){ 
						// must be common field 
						flag1 = true; 
					}else{ 
						// must be upload field 
						String realFileName = fileItem.getName(); 
						int index = realFileName.lastIndexOf("\\"); 
						if(index>=0){ 
							//IE6 浏览器 
							realFileName = realFileName.substring(index+1); 
						} 
						String uuidFileName = makeUuidFileName(realFileName); 
						InputStream is = fileItem.getInputStream();	 
						file = new File(uploadPath+"/"+category); 
						if(!file.exists()){ 
							file.mkdir(); 
						} 
						OutputStream os = new FileOutputStream(uploadPath+"/"+category+"/"+uuidFileName); 
						byte[] buf = new byte[1024]; 
						int len = 0; 
						while((len=is.read(buf))>0){ 
							os.write(buf,0,len); 
						} 
						is.close(); 
						os.close(); 
						fileItem.delete(); 
						flag1 = true; 
					} 
				} 
			} catch (FileUploadException e) { 
				e.printStackTrace(); 
			} 
		} 
		return flag1; 
	} 
 
	private String makeUuidFileName(String realFileName) { 
		return UUID.randomUUID().toString()+"_"+realFileName; 
	} 
}