|
|
|
@@ -1,25 +1,27 @@ |
|
|
|
package com.simple.controller; |
|
|
|
|
|
|
|
|
|
|
|
import com.simple.common.ResultData; |
|
|
|
import com.simple.utils.Constant; |
|
|
|
import io.swagger.annotations.Api; |
|
|
|
import io.swagger.annotations.ApiOperation; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestMethod; |
|
|
|
import org.springframework.web.bind.annotation.ResponseBody; |
|
|
|
import org.springframework.web.bind.annotation.RequestParam; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import java.io.BufferedOutputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.util.*; |
|
|
|
import com.simple.common.ResultData; |
|
|
|
|
|
|
|
import io.swagger.annotations.Api; |
|
|
|
import io.swagger.annotations.ApiOperation; |
|
|
|
|
|
|
|
@RestController |
|
|
|
@RequestMapping(value = "upload") |
|
|
|
@@ -27,6 +29,12 @@ import java.util.*; |
|
|
|
public class UploadController { |
|
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(UploadController.class); |
|
|
|
|
|
|
|
@Value("${fileUpload.path}") |
|
|
|
private String filePath; |
|
|
|
|
|
|
|
@Value("${fileUpload.server}") |
|
|
|
private String server; |
|
|
|
|
|
|
|
/** |
|
|
|
* 上传文件 |
|
|
|
@@ -35,40 +43,58 @@ public class UploadController { |
|
|
|
* @return |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST) |
|
|
|
@ResponseBody |
|
|
|
@PostMapping(value = "/fileUpload", consumes="multipart/*" |
|
|
|
,headers="content-type=multipart/form-data" ) |
|
|
|
@ApiOperation("上传文件") |
|
|
|
public ResultData fileUpload(HttpServletRequest request) { |
|
|
|
MultipartHttpServletRequest params=((MultipartHttpServletRequest) request); |
|
|
|
MultiValueMap<String, MultipartFile> iter = params.getMultiFileMap(); |
|
|
|
BufferedOutputStream stream = null; |
|
|
|
ArrayList<String> fileNameArray = new ArrayList<String>(); |
|
|
|
for (Map.Entry<String, List<MultipartFile>> entry : iter.entrySet()) { |
|
|
|
//取得上传文件 |
|
|
|
List<MultipartFile> files = params.getFiles(entry.getKey()); |
|
|
|
for(MultipartFile file: files) { |
|
|
|
if (!file.isEmpty()) { |
|
|
|
String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename(); |
|
|
|
String filePath = Constant.fileDirectory + File.separator + fileName; |
|
|
|
try { |
|
|
|
byte[] bytes = file.getBytes(); |
|
|
|
stream = new BufferedOutputStream(new FileOutputStream(new File(filePath))); |
|
|
|
stream.write(bytes); |
|
|
|
stream.close(); |
|
|
|
fileNameArray.add(fileName); |
|
|
|
} catch (Exception e) { |
|
|
|
stream = null; |
|
|
|
logger.error(e.getMessage()); |
|
|
|
return new ResultData(500, e.getMessage()); |
|
|
|
} |
|
|
|
} else { |
|
|
|
logger.error("You failed to upload " + file.getOriginalFilename() + "| size=" + file.getSize() + " because the file was empty."); |
|
|
|
return new ResultData(404, "You failed to upload " + file.getOriginalFilename() + " because the file was empty."); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
Map map = new HashMap(); |
|
|
|
map.put("imgarray", fileNameArray); |
|
|
|
return new ResultData(200, "上传成功", map); |
|
|
|
public ResultData fileUpload(@RequestParam("file") MultipartFile multiReq |
|
|
|
) { |
|
|
|
ResultData data = new ResultData(); |
|
|
|
FileOutputStream fos=null; |
|
|
|
FileInputStream fs=null; |
|
|
|
try { |
|
|
|
File targetFile = new File(filePath); |
|
|
|
if(!targetFile.exists()){ |
|
|
|
targetFile.mkdirs(); |
|
|
|
} |
|
|
|
String fileName = UUID.randomUUID().toString(); |
|
|
|
int dot = multiReq.getOriginalFilename().lastIndexOf('.'); |
|
|
|
fileName=fileName+ multiReq.getOriginalFilename().substring(dot, multiReq.getOriginalFilename().length()); |
|
|
|
fos = new FileOutputStream(new File(filePath+File.separator+fileName)); |
|
|
|
fs=(FileInputStream) multiReq.getInputStream(); |
|
|
|
byte[] buffer=new byte[1024]; |
|
|
|
int len=0; |
|
|
|
while((len=fs.read(buffer))!=-1){ |
|
|
|
fos.write(buffer, 0, len); |
|
|
|
} |
|
|
|
fos.close(); |
|
|
|
fs.close(); |
|
|
|
data.code=ResultData.SUCCESS; |
|
|
|
Map<String,String> map = new HashMap<>(); |
|
|
|
map.put("imgarray", server+"/"+fileName); |
|
|
|
data.data=map; |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
data.code=ResultData.ERROR; |
|
|
|
}finally { |
|
|
|
if(fos!=null) { |
|
|
|
try { |
|
|
|
fos.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
if(fs!=null) { |
|
|
|
try { |
|
|
|
fs.close(); |
|
|
|
}catch(IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
return data; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |