⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadandinsertsecondleveltitleservlet.java

📁 用myeclipse编写的一个简单新闻发布
💻 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("<body bgcolor='#66CC66'><h2>请先登陆!<h2></body>");
		} else if (islanded.equals("false")) {
			out.print("<body bgcolor='#66CC66'><h2>请输入正确的用户名与密码登陆!</h2></body>");
		} 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("<body bgcolor='#66CC66'><h2>文件上传成功!</h2></body>");
			} catch (Exception e) {
				System.out.println(e.getMessage());
				out.print("<body bgcolor='#66CC66'><h2>文件上传失败!</h2><a href='IssueSecondLevelTitleServlet'>返回</a></body>");
				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("<body><h2>二级标题发布成功!</h2><a href='IssueSecondLevelTitleServlet'>返回</a></body>");
				
			} else {
				out.print("<body bgcolor='#66CC66'><h2>二级标题发布失败!</h2><a href='IssueSecondLevelTitleServlet'>返回</a></body>");
			}
		}

	}

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

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -