IOS後台檔案上傳

來源:互聯網
上載者:User

標籤:http   io   ar   os   sp   for   檔案   on   log   

public ModelAndView GetImage(HttpServletRequest request,
   HttpServletResponse response) throws Exception {
  Map<String, Object> maps = new HashMap<String, Object>();

  try {
   // 建立一個臨時檔案存放要上傳的檔案,第一個參數為上傳檔案大小,第二個參數為存放的臨時目錄
   DiskFileItemFactory factory = new DiskFileItemFactory(
     1024 * 1024 * 5, new File("D:\\temp1"));
   // 設定緩衝區大小為 5M
   factory.setSizeThreshold(1024 * 1024 * 5);
   // 建立一個檔案上傳的控制代碼
   ServletFileUpload upload = new ServletFileUpload(factory);

   // 設定上傳檔案的整個大小和上傳的單個檔案大小
   upload.setSizeMax(1024 * 1024 * 50);
   upload.setFileSizeMax(1024 * 1024 * 5);

   try {
    // 把頁面表單中的每一個表單元素解析成一個FileItem
    List<FileItem> items = upload.parseRequest(request);
    for (FileItem fileItem : items) {
     // 如果是一個普通的表單元素(type不是file的表單元素)
     if (fileItem.isFormField()) {
      System.out.println(fileItem.getFieldName()); // 得到對應表單元素的名字
      System.out.println(fileItem.getString()); // 得到表單元素的值
     } else {

      // 擷取檔案的尾碼名
      String fileName = fileItem.getName();// 得到檔案的名字
      String fileExt = fileName.substring(
        fileName.lastIndexOf(".") + 1,
        fileName.length());
      String a = System.currentTimeMillis() + "";
      String path = request.getRealPath("/") + "kmtp/"
        + "IOS" + a + ".jpg";
      String url = "http://" + request.getServerName() + ":"
        + request.getServerPort() + "/"
        + request.getContextPath() + "kmtp/" + "IOS"
        + a + ".jpg";
      String extension = path.substring(
        path.lastIndexOf(".") + 1, path.length());
      Pattern pattern = Pattern.compile("bmp|gif|gepg|png|");
      Matcher matcher = pattern.matcher(extension);
      if (matcher.find()) {
       try {

        fileItem.write(new File(path));
        maps.put("message", url);
       } catch (Exception e) {
        e.printStackTrace();
        maps.put("message", "0");
       }
      } else {
       System.out.println("該檔案類型不能夠上傳");
       maps.put("message", "0");
      }
     }
    }
   } catch (FileUploadBase.SizeLimitExceededException e) {
    System.out.println("整個請求的大小超過了規定的大小...");
    maps.put("message", "0");
   } catch (FileUploadBase.FileSizeLimitExceededException e) {
    System.out.println("請求中一個上傳檔案的大小超過了規定的大小...");
    maps.put("message", "0");
   } catch (FileUploadException e) {
    e.printStackTrace();
    maps.put("message", "0");
   }

  } catch (Exception e) {
   logger.error(e.getStackTrace());
   e.printStackTrace();
   maps.put("message", "0");

  }
  // 返回結果 0系統錯誤,
  response.getWriter().write(
    URLEncoder.encode(JSONObject.fromObject(maps).toString(),
      "UTF-8"));
  return null;
 }

IOS後台檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.