<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] # 前提 1. form表單的enctype取值必須是:multipart/form-data (默認值是:application/x-www-form-urlencoded) enctype:是表單請求正文的類型 2. method屬性取值必須是Post 3. 提供一個文件選擇域`<input type=”file” />` # 文件上傳的原理分析 當 form 表單的 enctype 取值不是默認值后,request.getParameter()將失效。 `enctype=”application/x-www-form-urlencoded”`時,form 表單的正文內容是: `key=value&key=value&key=value` 當 form 表單的 enctype 取值為 `Mutilpart/form-data` 時,請求正文內容就變成: 每一部分都是 MIME 類型描述的正文 ![](https://img.kancloud.cn/13/e3/13e3e67050613049efa0eeef8679c46f_585x59.png) ![](https://img.kancloud.cn/63/85/6385086b83a4627aea65f0858257ac61_818x220.png) # 傳統方式 ~~~ @RequestMapping("/fileupload1") public String fileuoload1(HttpServletRequest request) throws Exception { System.out.println("文件上傳..."); // 使用fileupload組件完成文件上傳 // 上傳的位置 String path = request.getSession().getServletContext().getRealPath("/uploads/"); // 判斷,該路徑是否存在 File file = new File(path); if(!file.exists()){ // 創建該文件夾 file.mkdirs(); } // 解析request對象,獲取上傳文件項 DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); // 解析request List<FileItem> items = upload.parseRequest(request); // 遍歷 for(FileItem item:items){ // 進行判斷,當前item對象是否是上傳文件項 if(item.isFormField()){ // 說明普通表單向 }else{ // 說明上傳文件項 // 獲取上傳文件的名稱 String filename = item.getName(); // 把文件的名稱設置唯一值,uuid String uuid = UUID.randomUUID().toString().replace("-", ""); filename = uuid+"_"+filename; // 完成文件上傳 item.write(new File(path,filename)); // 刪除臨時文件 item.delete(); } } return "success"; } ~~~ # springmvc方式 ~~~ @RequestMapping("/fileupload2") public String fileuoload2(HttpServletRequest request, MultipartFile upload) throws Exception { System.out.println("springmvc文件上傳..."); // 使用fileupload組件完成文件上傳 // 上傳的位置 String path = request.getSession().getServletContext().getRealPath("/uploads/"); // 判斷,該路徑是否存在 File file = new File(path); if(!file.exists()){ // 創建該文件夾 file.mkdirs(); } // 說明上傳文件項 // 獲取上傳文件的名稱 String filename = upload.getOriginalFilename(); // 把文件的名稱設置唯一值,uuid String uuid = UUID.randomUUID().toString().replace("-", ""); filename = uuid+"_"+filename; // 完成文件上傳 upload.transferTo(new File(path,filename)); return "success"; } ~~~ **配置文件解析器** ~~~ <!-- 配置文件上傳解析器 --> <bean id="multipartResolver" <!-- id 的值是固定的--> class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 設置上傳文件的最大尺寸為 5MB --> <property name="maxUploadSize"> <value>5242880</value> </property> </bean> ~~~ 注意: 文件上傳的解析器 id 是固定的,不能起別的名稱,否則無法實現請求參數的綁定。(不光是文件,其他 字段也將無法綁定) # 跨服務器上傳 要用client對象 ~~~ @RequestMapping("/fileupload3") public String fileuoload3(MultipartFile upload) throws Exception { System.out.println("跨服務器文件上傳..."); // 定義上傳文件服務器路徑 String path = "http://localhost:9090/uploads/"; // 說明上傳文件項 // 獲取上傳文件的名稱 String filename = upload.getOriginalFilename(); // 把文件的名稱設置唯一值,uuid String uuid = UUID.randomUUID().toString().replace("-", ""); filename = uuid+"_"+filename; // 創建客戶端的對象 Client client = Client.create(); // 和圖片服務器進行連接 WebResource webResource = client.resource(path + filename); // 上傳文件 webResource.put(upload.getBytes()); return "success"; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看