1. 程式人生 > 實用技巧 >使用vs code 建立第一個flutter web 專案hello_world

使用vs code 建立第一個flutter web 專案hello_world

檔案上傳與下載

1.1 檔案上傳

案例:

登錄檔單/儲存商品等相關模組!

--à 註冊選擇頭像 / 商品圖片

(資料庫:儲存圖片路徑 / 圖片儲存到伺服器中指定的目錄)

檔案上傳,要點:

前臺:

1. 提交方式:post

2. 表單中有檔案上傳的表單項: <input type=”file” />

3. 指定表單型別:

預設型別:enctype="application/x-www-form-urlencoded"

檔案上傳型別:multipart/form-data

手動實現檔案上傳

<body>    
       <form name="frm_test"
action="${pageContext.request.contextPath }/upload" method="post" enctype="multipart/form-data"> 使用者名稱:<input type="text" name="userName"> <br/> 檔案: <input type="file" name="file_img"> <br/> <input type="submit" value="註冊"
> </form> </body>
public class UploadServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*
        request.getParameter(""); // GET/POST
        request.getQueryString(); // 獲取GET提交的資料 
        request.getInputStream(); // 獲取post提交的資料   
*/ /***********手動獲取檔案上傳表單資料************/ //1. 獲取表單資料流 InputStream in = request.getInputStream(); //2. 轉換流 InputStreamReader inStream = new InputStreamReader(in, "UTF-8"); //3. 緩衝流 BufferedReader reader = new BufferedReader(inStream); // 輸出資料 String str = null; while ((str = reader.readLine()) != null) { System.out.println(str); } // 關閉 reader.close(); inStream.close(); in.close(); }

輸出結果:

------WebKitFormBoundaryGoQviatB7iM1dhPr
Content-Disposition: form-data; name="userName"       【FileItem】

Jack
------WebKitFormBoundaryGoQviatB7iM1dhPr
Content-Disposition: form-data; name="file_img"; filename="reamde.txt"
Content-Type: text/plain                                     【FileItem】


test!!!!!!!!!!!!!
test!!!!!!!!!!!!!
------WebKitFormBoundaryGoQviatB7iM1dhPr--

總結:

最終獲取資料,要對上面的結果進行解析!

檔案上傳,在開發中經常用,每次都寫解析程式!(工具類)

也可以使用開源的檔案上傳元件-FileUpload元件!

Apache提供的檔案上傳元件:FileUpload元件

檔案上傳功能開發中比較常用,apache也提供了檔案上傳元件!

FileUpload元件:

1. 下載原始碼

2. 專案中引入jar檔案

commons-fileupload-1.2.1.jar 【檔案上傳元件核心jar包】

commons-io-1.4.jar 【封裝了對檔案處理的相關工具類】

public class UploadServlet extends HttpServlet {

    // upload目錄,儲存上傳的資源
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        /*********檔案上傳元件: 處理檔案上傳************/
        
        
        try {
            // 1. 檔案上傳工廠
            FileItemFactory factory = new DiskFileItemFactory();
            // 2. 建立檔案上傳核心工具類
            ServletFileUpload upload = new ServletFileUpload(factory);
        
            // 一、設定單個檔案允許的最大的大小: 30M
            upload.setFileSizeMax(30*1024*1024);
            // 二、設定檔案上傳表單允許的總大小: 80M
            upload.setSizeMax(80*1024*1024);
            // 三、 設定上傳表單檔名的編碼
            // 相當於:request.setCharacterEncoding("UTF-8");
            upload.setHeaderEncoding("UTF-8");
            
            
            // 3. 判斷: 當前表單是否為檔案上傳表單
            if (upload.isMultipartContent(request)){
                // 4. 把請求資料轉換為一個個FileItem物件,再用集合封裝
                List<FileItem> list = upload.parseRequest(request);
                // 遍歷: 得到每一個上傳的資料
                for (FileItem item: list){
                    // 判斷:普通文字資料
                    if (item.isFormField()){
                        // 普通文字資料
                        String fieldName = item.getFieldName();    // 表單元素名稱
                        String content = item.getString();        // 表單元素名稱, 對應的資料
                        //item.getString("UTF-8");  指定編碼
                        System.out.println(fieldName + " " + content);
                    }
                    // 上傳檔案(檔案流) ----> 上傳到upload目錄下
                    else {
                        // 普通文字資料
                        String fieldName = item.getFieldName();    // 表單元素名稱
                        String name = item.getName();            // 檔名                
                        String content = item.getString();        // 表單元素名稱, 對應的資料
                        String type = item.getContentType();    // 檔案型別
                        InputStream in = item.getInputStream(); // 上傳檔案流
                        
                        /*
                         *  四、檔名重名
                         *  對於不同使用者readme.txt檔案,不希望覆蓋!
                         *  後臺處理: 給使用者新增一個唯一標記!
                         */
                        // a. 隨機生成一個唯一標記
                        String id = UUID.randomUUID().toString();
                        // b. 與檔名拼接
                        name = id +"#"+ name;
                        
                        // 獲取上傳基路徑
                        String path = getServletContext().getRealPath("/upload");
                        // 建立目標檔案
                        File file = new File(path,name);
                        
                        // 工具類,檔案上傳
                        item.write(file);
                        item.delete();   //刪除系統產生的臨時檔案
                        
                        System.out.println();
                    }
                    
                }
                
            }
            else {
                System.out.println("當前表單不是檔案上傳表單,處理失敗!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
}

檔案上傳與下載,完整案例:

步驟:

1. 檔案上傳

2. 列表下載

Index.jsp

<body>    
          <a href="${pageContext.request.contextPath }/upload.jsp">檔案上傳</a> &nbsp;&nbsp;&nbsp;
          <a href="${pageContext.request.contextPath }/fileServlet?method=downList">檔案下載</a> 
          
  </body>

Upload.jsp

<body>    
       <form name="frm_test" action="${pageContext.request.contextPath }/fileServlet?method=upload" method="post" enctype="multipart/form-data">
            <%--<input type="hidden" name="method" value="upload">--%>
            
            使用者名稱:<input type="text" name="userName">  <br/>
           檔案:   <input type="file" name="file_img">   <br/>
           
           <input type="submit" value="提交">
        </form>
  </body>

Downlist.jsp

<body>    
    <table border="1" align="center">
        <tr>
            <th>序號</th>
            <th>檔名</th>
            <th>操作</th>
        </tr>
        <c:forEach var="en" items="${requestScope.fileNames}" varStatus="vs">
            <tr>
                <td>${vs.count }</td>
                <td>${en.value }</td>
                <td>
                    <%--<a href="${pageContext.request.contextPath }/fileServlet?method=down&..">下載</a>--%>
                    <!-- 構建一個地址  -->
                    <c:url var="url" value="fileServlet">
                        <c:param name="method" value="down"></c:param>
                        <c:param name="fileName" value="${en.key}"></c:param>
                    </c:url>
                    <!-- 使用上面地址 -->
                    <a href="${url }">下載</a>
                </td>
            </tr>
        </c:forEach>
    </table>          
  </body>

FileServlet.java

/**
 * 處理檔案上傳與下載
 * @author Jie.Yuan
 *
 */
public class FileServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 獲取請求引數: 區分不同的操作型別
        String method = request.getParameter("method");
        if ("upload".equals(method)) {
            // 上傳
            upload(request,response);
        }
        
        else if ("downList".equals(method)) {
            // 進入下載列表
            downList(request,response);
        }
        
        else if ("down".equals(method)) {
            // 下載
            down(request,response);
        }
    }
    
    
    /**
     * 1. 上傳
     */
    private void upload(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        try {
            // 1. 建立工廠物件
            FileItemFactory factory = new DiskFileItemFactory();
            // 2. 檔案上傳核心工具類
            ServletFileUpload upload = new ServletFileUpload(factory);
            // 設定大小限制引數
            upload.setFileSizeMax(10*1024*1024);    // 單個檔案大小限制
            upload.setSizeMax(50*1024*1024);        // 總檔案大小限制
            upload.setHeaderEncoding("UTF-8");        // 對中文檔案編碼處理

            // 判斷
            if (upload.isMultipartContent(request)) {
                // 3. 把請求資料轉換為list集合
                List<FileItem> list = upload.parseRequest(request);
                // 遍歷
                for (FileItem item : list){
                    // 判斷:普通文字資料
                    if (item.isFormField()){
                        // 獲取名稱
                        String name = item.getFieldName();
                        // 獲取值
                        String value = item.getString();
                        System.out.println(value);
                    } 
                    // 檔案表單項
                    else {
                        /******** 檔案上傳 ***********/
                        // a. 獲取檔名稱
                        String name = item.getName();
                        // ----處理上傳檔名重名問題----
                        // a1. 先得到唯一標記
                        String id = UUID.randomUUID().toString();
                        // a2. 拼接檔名
                        name = id + "#" + name;                        
                        
                        // b. 得到上傳目錄
                        String basePath = getServletContext().getRealPath("/upload");
                        // c. 建立要上傳的檔案物件
                        File file = new File(basePath,name);
                        // d. 上傳
                        item.write(file);
                        item.delete();  // 刪除元件執行時產生的臨時檔案
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
            
        
    }

    
    /**
     * 2. 進入下載列表
     */
    private void downList(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        // 實現思路:先獲取upload目錄下所有檔案的檔名,再儲存;跳轉到down.jsp列表展示
        
        //1. 初始化map集合Map<包含唯一標記的檔名, 簡短檔名>  ;
        Map<String,String> fileNames = new HashMap<String,String>();
        
        //2. 獲取上傳目錄,及其下所有的檔案的檔名
        String bathPath = getServletContext().getRealPath("/upload");
        // 目錄
        File file = new File(bathPath);
        // 目錄下,所有檔名
        String list[] = file.list();
        // 遍歷,封裝
        if (list != null && list.length > 0){
            for (int i=0; i<list.length; i++){
                // 全名
                String fileName = list[i];
                // 短名
                String shortName = fileName.substring(fileName.lastIndexOf("#")+1);
                // 封裝
                fileNames.put(fileName, shortName);
            }
        }
        
        // 3. 儲存到request域
        request.setAttribute("fileNames", fileNames);
        // 4. 轉發
        request.getRequestDispatcher("/downlist.jsp").forward(request, response);

    }

    
    /**
     *  3. 處理下載
     */
    private void down(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        // 獲取使用者下載的檔名稱(url地址後追加資料,get)
        String fileName = request.getParameter("fileName");
        fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");
        
        // 先獲取上傳目錄路徑
        String basePath = getServletContext().getRealPath("/upload");
        // 獲取一個檔案流
        InputStream in = new FileInputStream(new File(basePath,fileName));
        
        // 如果檔名是中文,需要進行url編碼
        fileName = URLEncoder.encode(fileName, "UTF-8");
        // 設定下載的響應頭
        response.setHeader("content-disposition", "attachment;fileName=" + fileName);
        
        // 獲取response位元組流
        OutputStream out = response.getOutputStream();
        byte[] b = new byte[1024];
        int len = -1;
        while ((len = in.read(b)) != -1){
            out.write(b, 0, len);
        }
        // 關閉
        out.close();
        in.close();
        
        
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }

}