- sad导入smartupload的jar包
- 提交到jsp页面上传
- 创建表单元素
-
<!--method="post" enctype="multipart/form-data" 必须写死--> <form action="upload.jsp" method="post" enctype="multipart/form-data"> 文件名:<input type="text" name="fileName"/><br/> 文件1:<input type="file" name="myFile1"/><br/> <!--文件2:<input type="file" name="myFile2"/><br/> --> <!--文件3:<input type="file" name="myFile3"/><br/> --> <!--文件4:<input type="file" name="myFile4"/><br/> --> <input type="submit" value="提交"/> </form>
-
- 提交到upload.jsp页面
-
<%@page import="com.jspsmart.upload.*"%> <%@page import="com.jspsmart.upload.SmartUpload"%> //获取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<files.getCount();i++){ File file=files.getFile(i);//获取第i个file String inputName=file.getFieldName();//<input/>标签的表单元素name属性的名称 String fileName=file.getFileName();//文件本身的名称 file.saveAs("img/"+fileName,smartUpload.SAVE_AUTO);//保存在tomcat服务器WebRoot下的img文件夹 file.saveAs("d://"+fileName,smartUpload.SAVE_AUTO);//保存到电脑本地d盘下 }
-
- 创建表单元素
- 提交到Servlet中上传
- 创建表单
-
<!--method="post" enctype="multipart/form-data" 必须写死--> <form action="SmartUploadServlet" method="post" enctype="multipart/form-data"> 文件名:<input type="text" name="fileName"/><br/> 文件1:<input type="file" name="myFile1"/><br/> <!--文件2:<input type="file" name="myFile2"/><br/> --> <!--文件3:<input type="file" name="myFile3"/><br/> --> <!--文件4:<input type="file" name="myFile4"/><br/> --> <input type="submit" value="提交"/> </form>
-
- 提交到SmartUploadServlet.java
-
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盘 }
-
- 创建表单
使用smartUpload进行文件的上传(两种情况:1.在另一个jsp页面取值上传,2.在servlet中取值上传)
最新推荐文章于 2022-04-10 18:46:11 发布