multipart上傳文件
A. springboot多文件上傳
MultipartFile提供了以下方法來獲取上傳文件的信息:
getOriginalFilename,獲取上傳的文件名字;
getBytes,獲取上傳文件內容,轉為位元組數組;
getInputStream,獲取一個InputStream;
isEmpty,文件上傳內容為空,或者根本就沒有文件上傳;
getSize,文件上傳的大小。
transferTo(File dest),保存文件到目標文件系統;
同時上傳多個文件,則使用MultipartFile數組類來接受多個文件上傳:
//多文件上傳 @RequestMapping(value = "/batch/upload", method = RequestMethod.POST)
@ResponseBody public String handleFileUpload(HttpServletRequest request){
List<MultipartFile> files = ((MultipartHttpServletRequest) request)
.getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i = 0; i < files.size(); ++i) {
file = files.get(i);
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
stream = new BufferedOutputStream(new FileOutputStream(
new File(file.getOriginalFilename())));
stream.write(bytes);
stream.close();
} catch (Exception e) {
stream = null;
return "You failed to upload " + i + " => " + e.getMessage();
}
} else {
return "You failed to upload " + i
+ " because the file was empty.";
}
}
return "upload successful";
}
可以通過配置application.properties對SpringBoot上傳的文件進行限定默認為如下配置:
spring.servlet.multipart.enabled=true
spring.servlet.multipart.file-size-threshold=0
spring.servlet.multipart.location=
spring.servlet.multipart.max-file-size=1MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.resolve-lazily=false
enabled默認為true,既允許附件上傳。
file-size-threshold限定了當上傳文件超過一定長度時,就先寫到臨時文件里。有助於上傳文件不佔用過多的內存,單位是MB或KB,默認0,既不限定閾值。
location指的是臨時文件的存放目錄,如果不設定,則web伺服器提供一個臨時目錄。
max-file-size屬性指定了單個文件的最大長度,默認1MB,max-request-size屬性說明單次HTTP請求上傳的最大長度,默認10MB.
resolve-lazily表示當文件和參數被訪問的時候再被解析成文件。
B. php curl 模擬表單數據流multipart/form-data上傳文件
在調用公眾號介面https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$token."&type=".$type;
上傳永久素材文件總是返回 "{\"errcode\":41005,\"errmsg\":\"media data missing\"}"
經過多次測試使用下面的方式,可以正常上傳
//調用測試
protected static $url;
protected static $delimiter;
protected static $instance;
public function index()
{
static::$delimiter = uniqid();
$basename = Request::instance()->root();
if (pathinfo($basename, PATHINFO_EXTENSION) == 'php') {
$basename = dirname($basename);
}
$result=$this->wxAddMaterial($token,$basename.'/upload/images/gnlog.jpg','image');
}
// 新增其他類型永久素材
public function wxAddMaterial($token,$filename='',$type='') {
// 設置請求參數
static::$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$token."&type=".$type;
$filePath = str_replace('\\', '/', $filename);
// 發送請求
$imginfo=pathinfo($filePath);
$fields = array(
'media'=>file_get_contents(".".$filePath),
'filename'=>$imginfo["basename"],
);
$res = $this->putPart( $fields);
// 發送請求
return $res;
}
//推送文件流
public function putPart($param) {
$post_data = static::buildData($param);
$curl = curl_init(static::$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Content-Type: multipart/form-data; boundary=" . static::$delimiter,
"Content-Length: " . strlen($post_data)
]);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
//編譯請求頭格式和數據流
private static function buildData($param){
$data = '';
$eol = "\r\n";
$upload = $param['media'];
unset($param['media']);
foreach ($param as $name => $content) {
$data .= "--" . static::$delimiter . "\r\n"
. 'Content-Disposition: form-data; name="' . $name . "\"\r\n\r\n"
. $content . "\r\n";
}
$data .= "--" . static::$delimiter . $eol
. 'Content-Disposition: form-data; name="media"; filename="' . $param['filename'] . '"' . "\r\n"
. 'Content-Type:application/octet-stream'."\r\n\r\n";
$data .= $upload . "\r\n";
$data .= "--" . static::$delimiter . "--\r\n";
return $data;
}
根據自己的實際情況稍作修改