www.pudn.com > NewsAssurance.rar > UploadAndInsertSecondLevelTitleServlet.java


package com.newsassurace.controller; 
 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.jsp.JspFactory; 
import javax.servlet.jsp.PageContext; 
 
import com.jspsmart.SmartRequest; 
import com.jspsmart.SmartUpload; 
import com.jspsmart.SmartUploadException; 
import com.newsassurace.biz.NewsBiz; 
import com.newsassurace.entity.SecondLevelTitleBean; 
 
public class UploadAndInsertSecondLevelTitleServlet extends HttpServlet { 
 
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException { 
 
		response.setContentType("text/html;charset=gb2312"); 
		request.setCharacterEncoding("gb2312"); 
		PrintWriter out = response.getWriter(); 
 
		String islanded = (String) request.getSession() 
				.getAttribute("islanded"); 
		if (islanded == null) { 
			out.print("

请先登陆!

"); } else if (islanded.equals("false")) { out.print("

请输入正确的用户名与密码登陆!

"); } else { // 使用了一个第三方的组件,存放在web-inf/lib下 response.setContentType("text/html;charset=GB2312"); // 由于SmartUpload的初始化方法需要pageContext,所以我们在servlet中得到他 // 为了得到pageConext要首先得到JspFactory的实例 // 通过JspFactory的实例的getPageContext方法得到pageConext的实例 JspFactory jf = null; // 得到JspFactory的实例 jf = JspFactory.getDefaultFactory(); // 下面是getPageContext方法参数的说明 /* * getPageContext(Servlet servlet, ServletRequest request, * ServletResponse response, java.lang.String errorPageURL, boolean * needsSession, int buffer, boolean autoflush) obtains an instance * of an implementation dependent javax.servlet.jsp.PageContext * abstract class for the calling Servlet and currently pending * request and response */ PageContext pageContext = jf.getPageContext(this, request, response, null, true, 8192, true); // 实例化SmartUpload SmartUpload mySmartUpload = new SmartUpload(); String fileparth = null;// 保存文件在服务器站点上的相对路径 try { // 初始化SmartUpload的实例,需要PageContext的实例 mySmartUpload.initialize(pageContext); // 设定最大上传的字节数,其实可以不进行设定,表示上传的文件没有大小限制 mySmartUpload.setTotalMaxFileSize(10000000); mySmartUpload.upload(); // 获取客户端文件上传到服务器 com.jspsmart.SmartFiles files = mySmartUpload.getFiles(); com.jspsmart.SmartFile file = files.getFile(0); String upLoadFileName = file.getFileName(); // 如果有文件上传就保存 if (!upLoadFileName.equals("") && upLoadFileName != null) { file.saveAs("/upload/" + file.getFileName()); } fileparth = "upload/" + file.getFileName();// 保存文件在服务器站点上的相对路径,不可以写"/upload/" out.print("

文件上传成功!

"); } catch (Exception e) { System.out.println(e.getMessage()); out.print("

文件上传失败!

返回"); return; } // 将二级标题存到数据库中 SmartRequest req = mySmartUpload.getRequest(); Date nowtime = new Date(); SimpleDateFormat formate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); String time = formate.format(nowtime);// 获得当前时间 String createrid = (String) request.getSession().getAttribute( "createrId");// 获得发布者id String parentname = req.getParameter("menu1"); String secondtitlename = req.getParameter("title"); NewsBiz nb = new NewsBiz(); String parentid = null; parentid = nb.queryParentId(parentname); SecondLevelTitleBean bean = new SecondLevelTitleBean(); bean.setTitleName(secondtitlename); bean.setFilePath(fileparth); bean.setCreaterId(createrid); bean.setCreateTime(time); bean.setParentId(parentid); NewsBiz biz = new NewsBiz(); boolean isinserted = false; isinserted = biz.saveSecondTitle(bean); if (isinserted) { out.print("

二级标题发布成功!

返回"); } else { out.print("

二级标题发布失败!

返回"); } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }