溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在java中接收ios文件

發(fā)布時間:2021-05-11 16:29:31 來源:億速云 閱讀:185 作者:Leah 欄目:編程語言

怎么在java中接收ios文件?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

Java的優(yōu)點是什么

1. 簡單,只需理解基本的概念,就可以編寫適合于各種情況的應(yīng)用程序;2. 面向?qū)ο螅?. 分布性,Java是面向網(wǎng)絡(luò)的語言;4. 魯棒性,java提供自動垃圾收集來進行內(nèi)存管理,防止程序員在管理內(nèi)存時容易產(chǎn)生的錯誤。;5. 安全性,用于網(wǎng)絡(luò)、分布環(huán)境下的Java必須防止病毒的入侵。6. 體系結(jié)構(gòu)中立,只要安裝了Java運行時系統(tǒng),就可在任意處理器上運行。7. 可移植性,Java可以方便地移植到網(wǎng)絡(luò)上的不同機器。8.解釋執(zhí)行,Java解釋器直接對Java字節(jié)碼進行解釋執(zhí)行。

ios Multipart/form-data POST請求java后臺spring接口一直出錯,搞了兩天,終于解決了,積累下來

package com.xx.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nupaApp.model.FileMeta;

@Controller
@RequestMapping("/controller")
public class File1Controller {

 LinkedList<FileMeta> files = new LinkedList<FileMeta>();
 FileMeta fileMeta = null;

 /***************************************************
  * URL: /rest/controller/upload upload(): receives files
  * 
  * @param request
  *   : MultipartHttpServletRequest auto passed
  * @param response
  *   : HttpServletResponse auto passed
  * @return LinkedList<FileMeta> as json format
  * @throws IOException
  * @throws FileUploadException
  ****************************************************/
 @RequestMapping(value = "/upload", method = RequestMethod.POST)
 @ResponseBody
 public String upload(HttpServletRequest request, HttpServletResponse response)
   throws IOException, FileUploadException {

  boolean isMultipart = ServletFileUpload.isMultipartContent(request);// 判斷是否是表單文件類型
  DiskFileItemFactory factory = new DiskFileItemFactory();
  ServletFileUpload sfu = new ServletFileUpload(factory);
  List items = sfu.parseRequest(request);// 從request得到所有上傳域的列表
  for (Iterator iter = items.iterator(); iter.hasNext();) {
   FileItem fileitem = (FileItem) iter.next();
   if (!fileitem.isFormField() && fileitem != null) {// 判讀不是普通表單域即是file
                // 操作fileitem文件步驟,可以獲取大小、路徑

    // 定義圖片輸出路徑
    String imgPath = "e:" + System.currentTimeMillis() + ".jpg";
    // 定義圖片流
    InputStream fin = fileitem.getInputStream();

    // 定義圖片輸出流
    FileOutputStream fout = new FileOutputStream(imgPath);
    // 寫文件
    byte[] b = new byte[1024];
    int length = 0;
    while ((length = fin.read(b)) > 0) {

     fout.write(b, 0, length);
    }

    // 關(guān)閉數(shù)據(jù)流
    fin.close();
    fout.close();
   }

  }

  return "200";
 }

}

pom.xml 添加

<!-- 這個用于上傳文件工具操作 -->
  <dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>1.3.1</version>
  </dependency>
  <dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>2.4</version>
  </dependency>

spring-config.xml 添加bean

<!-- 配置文件上傳,如果沒有使用文件上傳可以不用配置,當然如果不配,那么配置文件 中也不必引入上傳組件包 -->
 <bean id="multipartResolver "
  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <!-- 默認編碼 -->
  <property name="defaultEncoding" value="utf-8" />
  <!-- 文件大小最大值 -->
  <property name="maxUploadSize" value="10485760000" />
  <!-- 內(nèi)存中的最大值 -->
  <property name="maxInMemorySize" value="40960" />
 </bean>

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI