www.pudn.com > Jsp-Blog.rar > AddPhotoServlet.java, change:2010-05-26,size:2258b


package myblog.servlet; 
 
 
import java.io.IOException; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import myblog.dao.AddPhotosDao; 
 
 
import com.oreilly.servlet.MultipartRequest; 
 
public class AddPhotoServlet extends HttpServlet { 
	private static final long serialVersionUID = -7825355637448948879L; 
 
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException { 
		doPost(request, response); 
	} 
 
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException { 
		String username = request.getSession().getAttribute("username") 
				.toString(); 
 
		// 指定上传文件的存储目录 
	 
		String webTempPath = "D:/workspace/myblog/WebRoot/uploadimgs"; 
 
		// 单个文件大小限制为10M Bytes 
		int maxPostSize = 100 * 1024 * 1024; 
 
		// 声明上传文件名所使用的编码,默认值为 ISO-8859-1, 
		// 若改为utf-8则支持中文名 
		String enCoding = "utf-8"; 
 
		// 产生一个新的MultipartRequest 对象 
		MultipartRequest mpr = new MultipartRequest(request, webTempPath, 
				maxPostSize, enCoding); 
 
		// 使用 MultipartRequest 对象获取请求参数,不能直接使用request获取 
		String photo_title = mpr.getParameter("photo_title"); 
		String fileName = mpr.getFilesystemName("filename"); 
		String description = mpr.getParameter("photo_content"); 
		String url = webTempPath +"/"+ fileName; 
 
		AddPhotosDao addPhoto = new AddPhotosDao(); 
		int num = addPhoto.repeat(fileName); 
		// 如果存在同名文件则进行覆盖,而数据库进行更新操作 
		if (num == 3) { 
				request.setAttribute("addphotomsg", "已存在同名文件,请更改文件名!!");		 
		} else { 
			int number = addPhoto.uploadPhoto(photo_title, description, url, 
					username, fileName); 
			if (number == 1) { 
				request.setAttribute("addphotomsg", "上传成功!!"); 
			} else { 
				request.setAttribute("addphotomsg", "上传失败!!"); 
			} 
		} 
		request.getRequestDispatcher("addphoto.jsp").forward(request, response); 
 
	} 
 
}