一 : Java 代碼
@RestController
@RequestMapping(value = "/v1/upload")
public class UploadController {
@Autowired
private IWechatBaseService iWechatService;
/**
* 圖片上傳
* @param files
* @param request
* @param response
* @return
*/
@RequestMapping(value="/image", method = RequestMethod.POST)
@ApiOperation("圖片上傳")
public JSONData uploadFile(MultipartFile[] files, HttpServletRequest request, HttpServletResponse response){
List<BaseParameterEntity> list = iWechatService.getListByCode("files_url");
String[] path = new String[2];
for (int i=0;i< list.size();i++) {
path[i]=list.get(i).getValue();
}
UploadEntity fileEntity = new UploadEntity();
try{
if((files != null && files.length>0)){
StringBuffer url = new StringBuffer();
for (int i=0;i<files.length;i++) {
if(!files[i].isEmpty()) {
String pathname = uploadAndGetPathOnly(files[i], request, i, path);
if (!StringUtils.isEmpty(pathname)) {
url.append(pathname).append(";");
}
}
}
//設置頁面路徑
fileEntity.setFiles(url.toString().substring(0, url.length()-1));
// String s =url.toString().substring(url.toString().lastIndexOf("/")+1);
// fileEntity.setName((s).substring(0, s.length() - 1));
//fileEntity.setVisit("http://183.134.200.15:8034/api/images/"+ ((s).substring(0, s.length() - 1)));
}
}catch (RRException e){
e.setMsg("上傳文件失敗");
throw e;
}
JSONData jsonData = new JSONData();
jsonData.setData(fileEntity);
return jsonData;
}
}
二 :數據庫配置
CREATE TABLE `p_base_parameter` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
`name` varchar(100) DEFAULT NULL COMMENT '字段名稱',
`code` varchar(100) DEFAULT NULL COMMENT '字段編號',
`value` varchar(100) DEFAULT NULL COMMENT '字段值',
`sort` int(11) DEFAULT '0' COMMENT '排序',
`dept_id` int(11) DEFAULT NULL COMMENT '部門ID',
`description` varchar(500) DEFAULT NULL COMMENT '描述',
`parent_id` int(11) DEFAULT NULL COMMENT '父id',
`creater` int(11) DEFAULT NULL COMMENT '創建人',
`create_date` datetime DEFAULT NULL COMMENT '創建時間',
`last_updater` int(11) DEFAULT NULL COMMENT '更新人',
`last_update_date` datetime DEFAULT NULL COMMENT '更新時間',
`status` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COMMENT='基礎參數表';
三 : Nginx 配置
location ~* \.(apk|gif|jpg|jpeg|png|bmp|doc|docx|ppt|pptx|pdf|xls|xlsx|mp3|mp4)$ {
root /usr/local/cci/files/;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
expires 10d;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_ignore_client_abort on;
}
四 :知識點拓展
在上述的Nginx 配置,可以發現,攔截所有的 apk|gif|jpg|jpeg|png|bmp|doc|docx|ppt|pptx|pdf|xls|xlsx|mp3|mp4 結尾的資源,導致當其他圖片的文件夾路徑必須在 /usr/local/cci/files/ ,那怎麼獨立配置呢? 如下 :
location ~ /wechat/.*\.(apk|gif|jpg|jpeg|png|bmp|doc|docx|ppt|pptx|pdf|xls|xlsx|mp3|mp4)$ {
root /usr/local/cci/files/;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
expires 10d;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_ignore_client_abort on;
}
這樣就可以指定攔截 /wechat 的資源路徑了
例如 : http://ip:80/wechat/images/15746735130920.png
注意 : 在nginx 上面配置文件路徑爲 : /usr/local/cci/files/wechat/images
五 : spring boot 上傳文件大小設置
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 5242880 bytes exceeded; nested exception is org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (5512783) exceeds the configured maximum (5242880)
servlet:
multipart:
max-file-size: 300000000
max-request-size: 300000000
413 Request Entity Too Large
nginx http{ }中設置:client_max_body_size 20m;
沒有臨時文件
org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. /tmp/tomcat.5787299030890470771.8034/work/Tomcat/localhost/ROOT/upload_5034069b_14d0_43b8_ab31_55b464831843_00000101.tmp (No such file or directory)
spring:
http:
multipart:
location: /data/upload_tmp