|
|
|
@ -7,6 +7,7 @@ import com.hfkj.model.ResponseData; |
|
|
|
|
import com.hfkj.service.FileUploadService; |
|
|
|
|
import io.swagger.annotations.Api; |
|
|
|
|
import io.swagger.annotations.ApiOperation; |
|
|
|
|
import net.coobird.thumbnailator.Thumbnails; |
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
@ -18,7 +19,9 @@ import javax.annotation.Resource; |
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
import java.io.FileOutputStream; |
|
|
|
|
import java.io.InputStream; |
|
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
|
|
@RestController |
|
|
|
@ -31,6 +34,9 @@ public class FileUploadController { |
|
|
|
|
@Resource |
|
|
|
|
private SysConfig sysConfig; |
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
private FileUploadService fileUploadService; |
|
|
|
|
|
|
|
|
|
@RequestMapping(value="/uploadfile",method = RequestMethod.POST) |
|
|
|
|
@ResponseBody |
|
|
|
|
@ApiOperation(value = "文件上传") |
|
|
|
@ -85,5 +91,64 @@ public class FileUploadController { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST) |
|
|
|
|
@ApiOperation(value = "上传文件(超过500KB压缩)") |
|
|
|
|
@ResponseBody |
|
|
|
|
public ResponseData fileUpload(@RequestParam(value = "files" , required = false) MultipartFile files, |
|
|
|
|
HttpServletRequest request, |
|
|
|
|
HttpServletResponse response |
|
|
|
|
) { |
|
|
|
|
try { |
|
|
|
|
response.setHeader("Access-Control-Allow-Origin", "*"); |
|
|
|
|
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( |
|
|
|
|
request.getSession().getServletContext()); |
|
|
|
|
// 判断 request 是否有文件上传,即多部分请求
|
|
|
|
|
List<String> fileNames = new ArrayList<String>(); |
|
|
|
|
if (multipartResolver.isMultipart(request)) { |
|
|
|
|
// 转换成多部分request
|
|
|
|
|
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; |
|
|
|
|
Iterator<String> iterator = multiRequest.getFileNames(); |
|
|
|
|
|
|
|
|
|
while (iterator.hasNext()) { |
|
|
|
|
MultipartFile file = multiRequest.getFile(iterator.next()); |
|
|
|
|
if (file != null) { |
|
|
|
|
FileOutputStream out = null; |
|
|
|
|
try { |
|
|
|
|
String fileType = file.getOriginalFilename() |
|
|
|
|
.substring(file.getOriginalFilename().lastIndexOf(".") + 1); |
|
|
|
|
String fileName = System.currentTimeMillis() + "." + fileType; |
|
|
|
|
String childPath = DateUtil.date2String(new Date(), "yyyyMM"); |
|
|
|
|
String destDirName = sysConfig.getFileUrl() + File.separator + childPath; |
|
|
|
|
File dir = new File(destDirName); |
|
|
|
|
if (!dir.exists()) { |
|
|
|
|
dir.mkdirs(); |
|
|
|
|
} |
|
|
|
|
out = new FileOutputStream(destDirName + File.separator + fileName); |
|
|
|
|
out.write(file.getBytes()); |
|
|
|
|
out.flush(); |
|
|
|
|
fileNames.add(childPath + "/" + fileName); |
|
|
|
|
// 图片压缩
|
|
|
|
|
InputStream fis = new FileInputStream(destDirName + File.separator + fileName); |
|
|
|
|
if (fis.available() > 500000) { |
|
|
|
|
Thumbnails.of(new FileInputStream(destDirName + File.separator + fileName)).scale(0.5).toFile(new File(destDirName + File.separator + fileName)); |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error(e.getMessage(), e); |
|
|
|
|
} finally { |
|
|
|
|
if (out != null) { |
|
|
|
|
out.close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return ResponseMsgUtil.success(fileNames); |
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error(e.getMessage(), e); |
|
|
|
|
return ResponseMsgUtil.exception(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|