|
|
|
@@ -0,0 +1,70 @@ |
|
|
|
package com.simple.controller; |
|
|
|
|
|
|
|
|
|
|
|
import com.simple.common.ResultData; |
|
|
|
import com.simple.utils.Constant; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.stereotype.Controller; |
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
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.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.*; |
|
|
|
|
|
|
|
@Controller |
|
|
|
@RequestMapping(value = "upload") |
|
|
|
public class UploadController { |
|
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(UploadController.class); |
|
|
|
|
|
|
|
/** |
|
|
|
* 上传文件 |
|
|
|
* |
|
|
|
* @param request |
|
|
|
* @return |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST) |
|
|
|
@ResponseBody |
|
|
|
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(0, "上传成功", map); |
|
|
|
} |
|
|
|
} |