一个使用jspSmartUpload控件上传文件的例子

一个使用jspSmartUpload控件上传文件的例子

view plaincopy to clipboardprint?
package cn.edu.hbcit.ips.servlet;  
import java.io.IOException;  
import java.io.PrintWriter;  
import java.util.*;  
import javax.servlet.*;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.apache.log4j.Logger;  
import cn.edu.hbcit.ips.bean.ConnBean;  
import cn.edu.hbcit.ips.bean.FileOperate;  
import com.jspsmart.upload.*;  
public class ExcelUpload extends HttpServlet {  
      private ServletConfig config;  
      private static final String CONTENT_TYPE = "text/html; charset=GB2312";  
      protected final Logger log = Logger.getLogger(ExcelUpload.class.getName());  
        
    /** 
     * Constructor of the object. 
     */ 
    public ExcelUpload() {  
        super();  
    }  
    /** 
     * Destruction of the servlet. <br> 
     */ 
    public void destroy() {  
        super.destroy(); // Just puts "destroy" string in log  
        // Put your code here  
    }  
    /** 
     * The doGet method of the servlet. <br> 
     * 
     * This method is called when a form has its tag value method equals to get. 
     *  
     * @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 doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        doPost(request, response);  
          
    }  
    /** 
     * 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 {  
        request.setCharacterEncoding("gb2312");  
        int countFile = 0;  
        String realPath = "";  
        String oldFilename ="";  
        SmartUpload su = new SmartUpload(); //实例化jspSmartUpload  
        su.initialize(config, request, response);// 初始化SmartUpload  
        su.setAllowedFilesList("xls"); //允许上传的文件类型(中间用,隔开)  
        su.setMaxFileSize(1 * 1024 * 1024); //声明上传文件最大容量  
        log.debug("Uploading...");  
          try{  
              su.upload();  
              com.jspsmart.upload.File file = su.getFiles().getFile(0);  
              oldFilename = file.getFileName();//得到原文件名  
              log.debug("原文件名为:"+oldFilename);  
              FileOperate fo = new FileOperate();  
                
              if(file.getFileExt().equalsIgnoreCase("xls")){//判断文件扩展名是否为xls  
                //将文件另存为  
                  if (!file.isMissing()) {  
                      realPath = request.getRealPath("/")+"UserFiles\\";  
//                    su.save(realPath);  
                      realPath += fo.generateRandomFilename()+"xls";  
                      log.debug("当前路径是:"+realPath);  
                        
                      file.saveAs(realPath, su.SAVE_PHYSICAL);  
                      log.debug("上传文件大小:"+file.getSize());  
                  }  
              }  
                
            }catch(Exception e){  
              log.error(e.getMessage());  
            }  
    }  
    /** 
     * Initialization of the servlet. <br> 
     * 
     * @throws ServletException if an error occurs 
     */ 
    public void init(ServletConfig config) throws ServletException {  
        this.config = config;  
    }  

package cn.edu.hbcit.ips.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import cn.edu.hbcit.ips.bean.ConnBean;
import cn.edu.hbcit.ips.bean.FileOperate;
import com.jspsmart.upload.*;
public class ExcelUpload extends HttpServlet {
  private ServletConfig config;
  private static final String CONTENT_TYPE = "text/html; charset=GB2312";
  protected final Logger log = Logger.getLogger(ExcelUpload.class.getName());
 
/**
* Constructor of the object.
*/
public ExcelUpload() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @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 doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);

}
/**
* 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 {
request.setCharacterEncoding("gb2312");
int countFile = 0;
String realPath = "";
String oldFilename ="";
    SmartUpload su = new SmartUpload(); //实例化jspSmartUpload
    su.initialize(config, request, response);// 初始化SmartUpload
    su.setAllowedFilesList("xls"); //允许上传的文件类型(中间用,隔开)
    su.setMaxFileSize(1 * 1024 * 1024); //声明上传文件最大容量
    log.debug("Uploading...");
      try{
          su.upload();
          com.jspsmart.upload.File file = su.getFiles().getFile(0);
          oldFilename = file.getFileName();//得到原文件名
          log.debug("原文件名为:"+oldFilename);
          FileOperate fo = new FileOperate();
         
          if(file.getFileExt().equalsIgnoreCase("xls")){//判断文件扩展名是否为xls
        //将文件另存为
          if (!file.isMissing()) {
          realPath = request.getRealPath("/")+"UserFiles\\";
//           su.save(realPath);
          realPath += fo.generateRandomFilename()+"xls";
          log.debug("当前路径是:"+realPath);
         
          file.saveAs(realPath, su.SAVE_PHYSICAL);
          log.debug("上传文件大小:"+file.getSize());
          }
          }
         
        }catch(Exception e){
          log.error(e.getMessage());
        }
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
}

你可能感兴趣的:(apache,bean,log4j,servlet)