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

📄 uploadservlet.java

📁 一个oa系统
💻 JAVA
字号:
package com.oa.uploadfile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//import com.hibernate.ResumeDAO;

public class UploadServlet extends HttpServlet {
	
	private ServletConfig config;
	/**
	 * Constructor of the object.
	 */
	public UploadServlet() {
		super();
	}
	

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		
			    SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
			    // 新建一个SmartUpload对象
			    SmartUpload su = new SmartUpload();
			    // 上传初始化 
			    su.initialize(request.getSession().getServletContext(),request,response);
			    // 设定上传限制
			    // 1.限制每个上传文件的最大长度。
			    //su.setMaxFileSize(10000);
			    // 2.设定允许上传的文件(通过扩展名限制)。
			    su.setAllowedFilesList("jpg,gif");
			    // 上传文件
			    try {
					su.upload();
				} catch (SmartUploadException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			    // 将上传文件保存到指定目录
				//for(int i=0;i<su.getFiles().getCount();i++){
			    SmartFile f=su.getFiles().getFile(0);
			    //指定路径
			    String filename=format.format(new java.util.Date())+f.getFileName();
			    String strPath="bbspicture/"+filename;
			    System.out.println(strPath);
			    try {
					f.saveAs(strPath,SmartUpload.SAVE_VIRTUAL);
				} catch (SmartUploadException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//response.sendRedirect("../fatie1.jsp");
				RequestDispatcher rr=request.getRequestDispatcher("fatie1.jsp");
		        rr.forward(request,response);
		       // document.location.href="../fatie1.jsp"; 

		        
		        
				/*//将上传文件入库				
				String strphotopath=dao.ByqueryPhoto(userid);
				if(!strphotopath.equalsIgnoreCase("")){
					String photo=request.getRealPath(strphotopath);//获得照片绝路径
		    		File fl=new File(photo); 
		    		if(fl.exists()){
		    			fl.delete();
		    		}
		    	//}
		    		*/
		    		
		    		
		    		
	}
	//}
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init(ServletConfig config) throws ServletException {
		// Put your code here
		this.config = config;
	}

}

⌨️ 快捷键说明

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