关于SmartUpload技术

关于SmartUpload技术
一、SmartUpload技术
(一)优点
1、使用简单。在JSP文件中仅仅书写三五行java代码就可以搞定文件的上传或下载,方便。

2、能全程控制上传。利用jspSmartUpload组件提供的对象及其操作方法,可以获得全部上传文件的信息(包括文件名,大小,类型,扩展名,文件数据等),方便存取。

3、能对上传的文件在大小、类型等方面做出限制。如此可以滤掉不符合要求的文件。

4、下载灵活。仅写两行代码,就能把Web服务器变成文件服务器。不管文件在Web服务器的目录下或在其它任何目录下,都可以利用jspSmartUpload进行下载。
(二)缺点
1、不适用于上传大文件
     使用smartupload上传过大文件,或者多文件的时候可能出现CPU或内存占用过高的问题.并且:只有重新启动容器才能恢复正常
(三)实例代码:
public class SmartFileUploadUtil {
      
          private ServletConfig config;
          HttpServletRequest request;
          HttpServletResponse response;
            //文件存储路径
            private static String  FILE_UPLOAD_PATH="F:/Javaworkspace/BookManagementSystem/WebContent/jsp图书售卖网站/bookImages/";
            //上传文件大小限制
            private static int FILE_SIZE_LIMIT=1024*1024*5;
            //上传所有文件大小限制
            private static int TOTAL_FILE_SIZE_LIMIT=1024*1024*100;
            //上传文件类型限制
            private static String FILE_TYPE_LIMIT="jpg,png";
            
            private static int result=0;
            
            public SmartFileUploadUtil(ServletConfig  config,HttpServletRequest request,HttpServletResponse response){
                  this.config=config;
                  this.request=request;
                  this.response=response;
            }
            
            public int smartFileUpload(){
                  try {
                        request.setCharacterEncoding("utf-8");
                  } catch (UnsupportedEncodingException e1) {
                        e1.printStackTrace();
                  }
                  response.setCharacterEncoding("UTF-8");
                  response.setContentType("text/html;charset=UTF-8");
                  //判断上传路径是否存在
                  File f=new File(FILE_UPLOAD_PATH);
                  if(!f.exists()){
                        f.mkdirs();
                  }
                  
                  SmartUpload su=new SmartUpload();
                  try {
                        //初始化smartupload对象
                        su.initialize(config,request,response);
                        //设置上传文件大小
                        su.setMaxFileSize(FILE_SIZE_LIMIT);
                        //设置上传所有文件大小
                        su.setTotalMaxFileSize(TOTAL_FILE_SIZE_LIMIT);
                        //限制上传文件类型
                        su.setAllowedFilesList(FILE_TYPE_LIMIT);
                        //上传文件
                        su.upload();
                        //返回成功上传文件数目
                        result=su.save(FILE_UPLOAD_PATH);
                        System.out.println("上传成功了"+result+"个文件!");
                  } catch (ServletException e) {
                        System.out.println("ServletException");
                        e.printStackTrace();
                  }catch (IOException e) {
                System.out.println("IOException");
                        e.printStackTrace();
                  } catch (SmartUploadException e) {
                        System.out.println("SmartUploadException");
                        e.printStackTrace();
                  }
                  return result;       
            }
}

Servlet类
public class FileUploadTest extends HttpServlet {
      private static final long serialVersionUID = 1L;
      int result=0;
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            SmartFileUploadUtil smartFileUploadUtil=new SmartFileUploadUtil(getServletConfig(),request,response);
            int result=smartFileUploadUtil.smartFileUpload();
        if(result>0){
            System.out.println("上传成功!");
        }
      }
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
      }
}
JSP form表单代码:
<form action="FileUploadTest"
method="post" enctype="multipart/form-data">
文件位置:<input type="file"name="mfile1"><br>
文件位置:<input type="file"name="mfile1"><br>
文件位置:<input type="file"name="mfile1"><br>
<input type="submit"value="提交">  
</form>

红色标记处为易错代码



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页