使用jspSmartUpload实现文件的上传

1.1.  使用jspSmartUpload实现文件的上传

1.1.1.  新建一个web工程

1.1.2.  上传页面index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>文件上传</title>
</head>
<body>
         <h3>文件上传</h3>
         <form action=" FileSmartUpload" method="post" enctype="multipart/form-data">
                   文件<input type="file" name="file1">
                   <input type="submit" value="提交">
         </form>
</body>
</html>


1.1.3.  文件上传处理FileSmartUpload.java

package com.jsp.file.upload.servlet;
 
import java.io.IOException;
import java.sql.SQLException;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
 
@WebServlet("/FileSmartUpload")
public class FileSmartUpload extends HttpServlet {
 
 
         private static final long serialVersionUID = 1L;
 
         public void doPost(HttpServletRequest request, HttpServletResponse response)
                            throws ServletException, IOException{
                  
                   // 创建文件上传对象
                   SmartUpload smartUpload = new SmartUpload();
                   // 初始化文件上传对象
                   smartUpload.initialize(this.getServletConfig(),request, response);
                   // 设置单个文件最大的字节数
                   smartUpload.setMaxFileSize(1000*100);
                   // 设置总文件的最大字节数
                   smartUpload.setTotalMaxFileSize(1000*1000*100);
                   // 设置允许的文件的扩展名
                   smartUpload.setAllowedFilesList("jpg,doc,rar,txt,java");
                   try {
                            // 设置不允许的文件的扩展名
                            smartUpload.setDeniedFilesList("exe,php");
                            // 上传文件
                            smartUpload.upload();
                            // 保存文件,指定路径即可
                            smartUpload.save("d://");
                           
                   } catch (SQLException e) {
                            e.printStackTrace();
                   } catch (SmartUploadException e) {
                            e.printStackTrace();
                   }
         }
 
}


你可能感兴趣的:(jsp,文件上传)