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" >