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

📄 newsupservlet.java

📁 基于j2ee的物流软件
💻 JAVA
字号:
package com.wuliu.upservlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartFile;
import com.jspsmart.upload.SmartRequest;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.wuliu.dao.NewsInfoDAO;
import com.wuliu.entity.NewsInfo;
import com.wuliu.entity.UUid;

public class Newsupservlet extends HttpServlet {

	/**
	 * 新闻上传 袁子鹏 2.19
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void init() {

	}
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException

	{	
		SmartUpload su = new SmartUpload();// 构造对象
		su.initialize(this.getServletConfig(), request, response);// 初始化
		su.setMaxFileSize(10000000);// 上传大小
		su.setAllowedFilesList("gif,jpg");// 设置格式限制
		String path = null;
		
			try {
				su.upload();
			} catch (SmartUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			SmartFile sf = su.getFiles().getFile(0);
			String fileName = sf.getFileName();
			String temp = fileName.substring(fileName.lastIndexOf("."));

			path = "images/news/" + UUid.getUID() + temp;
			try {
				sf.saveAs(path, SmartFile.SAVEAS_VIRTUAL);
			} catch (SmartUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}// 另存为
		//request.setAttribute("path", path);//放入文件路径
        //插入数据库
	    SmartRequest sRequest=su.getRequest();
		UpServlet.insertFile(path, sRequest);
		request.getRequestDispatcher("newsShow.do?page=0")
				.forward(request, response);

	}
	public final static void insertFile(String path,SmartRequest request){
		
		String newsTitle = request.getParameter("newsTitle");
		String newsContent = request.getParameter("newsContent");
		String newsAuthor = request.getParameter("newsAuthor");
		String newsRemark = request.getParameter("newsRemark");
	    String newsPic = path;	
		NewsInfo ni = new NewsInfo(newsTitle,newsContent,newsAuthor,newsPic,newsRemark);
		NewsInfoDAO ndao = new NewsInfoDAO();
		ndao.insertNewsInfo(ni);
		
	}

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

⌨️ 快捷键说明

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