AddNews.java
上传用户:junmaots
上传日期:2022-07-09
资源大小:2450k
文件大小:3k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. /*
  2.  * Created on 2005-12-10
  3.  *
  4.  * TODO To change the template for this generated file go to
  5.  * Window - Preferences - Java - Code Style - Code Templates
  6.  */
  7. package com.mycompany.servlet;
  8. import java.io.IOException;
  9. import java.net.URLEncoder;
  10. import java.text.ParseException;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Date;
  13. import javax.servlet.ServletConfig;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.http.HttpServlet;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. import com.jspsmart.upload.Request;
  19. import com.jspsmart.upload.SmartUpload;
  20. import com.jspsmart.upload.SmartUploadException;
  21. import com.mycompany.news.dto.News;
  22. import com.mycompany.news.service.NewsService;
  23. /**
  24.  * @author Administrator
  25.  *
  26.  * TODO To change the template for this generated type comment go to
  27.  * Window - Preferences - Java - Code Style - Code Templates
  28.  */
  29. public class AddNews extends HttpServlet {
  30. private ServletConfig config;
  31. public void init(ServletConfig arg0) throws ServletException {
  32. config = arg0;
  33. }
  34. protected void service(HttpServletRequest request, HttpServletResponse response)
  35. throws ServletException, IOException {
  36. SmartUpload upload = new SmartUpload();
  37. try
  38.     {
  39. upload.initialize(config, request, response);
  40. // 允许上传的文件类型
  41. // upload.setAllowedFilesList("doc,xls,");
  42. // 拒绝上传的文件类型
  43. // upload.setDeniedFilesList("exe,bat,jsp");
  44. // 允许上传文件的单个最大大小
  45. upload.setMaxFileSize(1024 * 1024 * 20);
  46. // 允许上传文件的最大大小总和
  47. // upload.setTotalMaxFileSize(1024*1024*10);
  48. //上传数据
  49. upload.upload();
  50. }
  51. catch (SmartUploadException e){
  52. e.printStackTrace();
  53. return;
  54. }
  55. Request req = upload.getRequest();
  56. Long columnId = Long.valueOf(req.getParameter("columnId"));
  57. String subject = req.getParameter("subject");
  58. String content = req.getParameter("content");
  59. String author =req.getParameter("author");
  60. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  61. Date showTime=new Date();
  62. try {
  63. showTime=sdf.parse(req.getParameter("showTime"));
  64. } catch (ParseException e1) {
  65. e1.printStackTrace();
  66. }
  67. String source = req.getParameter("source");
  68. News news = new News();
  69. news.setAuthor(author);
  70. news.setColumnId(columnId);
  71. news.setContent(content);
  72. news.setNewsStatus(new Integer(0));
  73. news.setShowTime(showTime);
  74. news.setSubject(subject);
  75. NewsService nservice = new NewsService();
  76. boolean result = nservice.addNews(news,upload);
  77. if(result)
  78. response.sendRedirect(request.getContextPath()+"/opsucc.jsp?message="+URLEncoder.encode(nservice.getMessage(),"GB2312"));
  79. else
  80. response.sendRedirect(request.getContextPath()+"/opfail.jsp?message="+URLEncoder.encode(nservice.getMessage(),"GB2312"));
  81. }
  82. }