smartUploadを使用してファイルのアップロードを行います(2つの場合:1.別のjspページで値を取ってアップロードし、2.servletで値を取ってアップロードします)
7003 ワード
// SmartUpload import="com.jspsmart.upload.SmartUpload"
SmartUpload smartUpload=new SmartUpload();
//
smartUpload.initialize(pageContext);
//
smartUpload.setAllowedFilesList("jpg,png,gif");
//
smartUpload.setDeniedFilesList("docx");
// (20M, 2M)
smartUpload.setMaxFileSize(1024*1024*20);
//
smartUpload.setTotalMaxFileSize(1024*1024*1024*2);
try{
smartUpload.upload();//
}catch(Exception e){
e.printStackTrace();
}
Files files=smartUpload.getFiles();// file
for(int i=0;i name
String fileName=file.getFileName();//
file.saveAs("img/"+fileName,smartUpload.SAVE_AUTO);// tomcat WebRoot img
file.saveAs("d://"+fileName,smartUpload.SAVE_AUTO);// d
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
// smartupload
SmartUpload smartUpload=new SmartUpload();
//pageContext jsp ,
// :
JspFactory _jspxFactory = JspFactory.getDefaultFactory();
PageContext pageContext = _jspxFactory.getPageContext(this,request,response,"",true,8192,true);
smartUpload.initialize(pageContext);//
// :
smartUpload.initialize(this, request, response);//
try {
smartUpload.upload();//
} catch (Exception e) {
e.printStackTrace();
}
Files files = smartUpload.getFiles();// file
for (int i = 0; i < files.getCount(); i++) {
File file = files.getFile(i);// i file( )
String fileName = file.getFileName();// ( )
String fieldName = file.getFieldName();// input name=" "
file.saveAs("img/"+fileName,smartUpload.SAVE_AUTO);// WebRoot img
file.saveAs("d:/"+fileName,smartUpload.SAVE_AUTO);// d
}