smartupload上传组件使用说明(servlet)

servlet :

 

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.ServletConfig;
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 com.jspsmart.upload.SmartUpload;

 

/**
 * Servlet implementation class for Servlet: LoadDataServlet
 *
 */
 public class LoadDataServlet extends HttpServlet{
 
 private static final Logger log = Logger.getLogger(LoadDataServlet.class);
 private ServletConfig config;
 
 
    /**
     * Init the servlet
     */
    final public void init(ServletConfig config) throws ServletException {
        this.config = config;
    }

    final public ServletConfig getServletConfig() {
        return config;
    }
 
    /* (non-Java-doc)
  * @see javax.servlet.http.HttpServlet#HttpServlet()
  */
 public LoadDataServlet() {
  super();
 }    
 
 /* (non-Java-doc)
  * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  this.doPost(request,response);
 }   
 
 /* (non-Java-doc)
  * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  
  log.info("com.glu.sdb.servlet.LoadDataServlet doPost() upload begin!");
  
  //upload component
        SmartUpload mySmartUpload = new SmartUpload();
        com.jspsmart.upload.Request _smartReq = null;
       
  try {
   //Initialization
            mySmartUpload.initialize(config, request, response);
            //set max file size 10M
            mySmartUpload.setMaxFileSize(10000000);
            // Upload
            mySmartUpload.upload();
        }
        catch (Exception suEX) {
            suEX.printStackTrace();
            log.warn("com.glu.sdb.servlet.LoadDataServlet doPost() upload failed!");
            return;
        }
       
        //get request parameters
        _smartReq = mySmartUpload.getRequest(); 
  
  
  //file path
  String filePath = null;
  //retailerId
  String retailerId = null;
  //distribId
  String distribId = null;
  //date string
  String reportDate = null;
  //actions
  String actions = null;
  //message
  String message = null;
  
  retailerId = _smartReq.getParameter("retailerId");
  distribId = _smartReq.getParameter("distribId");
  reportDate = _smartReq.getParameter("reportDate");
  actions = _smartReq.getParameter("actions");
  
  //upload file
     BufferedOutputStream bout = null;
     
     String realPath = getServletContext().getRealPath("/");
     filePath = realPath + "temp" + "/" + new Long(System.currentTimeMillis()).toString() + ".xls";
  
  com.jspsmart.upload.File _smartFile = mySmartUpload.getFiles().getFile(0);
  
  //read client excel file to server
  byte[] binary_ = new byte[_smartFile.getSize()];
        for (int j = 0; j < _smartFile.getSize(); j++) {
            binary_[j] = (byte) _smartFile.getBinaryData(j);
        }
       
        try{
         bout = new BufferedOutputStream(new FileOutputStream(filePath));
         
         log.info("read client excel to server begin!");
         bout.write(binary_);
         log.info("read client excel to server end!");
         
        }catch(Exception e){
         log.info("read client excel to server failed!" + e.getMessage());
        }finally{
         if(bout != null) bout.close();
        }  
 }          
}

 

 

 

jsp文件:

 

<form id='form1' name='form1' method='post' enctype="multipart/form-data" >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值