Commit 57aac7ac authored by hezhen's avatar hezhen

修改上传图片

parent 27b6a8f9
package com.xxfc.platform.universal.biz;
import com.alibaba.fastjson.JSONObject;
import com.github.wxiaoqi.security.common.util.OrderUtil;
import com.github.wxiaoqi.security.common.util.process.ResultCode;
import com.github.wxiaoqi.security.common.util.process.SystemConfig;
import com.github.wxiaoqi.security.common.util.result.JsonResultUtil;
import com.xxfc.platform.universal.controller.OrderRefundController;
import com.xxfc.platform.universal.vo.OrderRefundVo;
import com.xxfc.platform.universal.weixin.api.WxPayRefundUtils;
import com.xxfc.platform.universal.weixin.util.OrderUtil;
import com.xxfc.platform.universal.weixin.util.Snowflake;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
......
......@@ -217,8 +217,10 @@ public class UploadController{
}
@RequestMapping(value = "/app/unauth/upload", method = RequestMethod.POST)
public JSONObject upload(@RequestParam("file") MultipartFile file)
throws Exception {
public JSONObject upload(
@RequestParam("file") MultipartFile file,
@RequestParam(value = "prefix",defaultValue = "app")String prefix
)throws Exception {
String contentType = file.getContentType(); //图片文件类型
// String fileName = file.getOriginalFilename(); //图片名字
/* if (!contentType.equals("image/jpeg") && !contentType.equals("image/gif")&&!contentType.equals("image/png")) {
......@@ -227,7 +229,7 @@ public class UploadController{
if (file.getSize() > MAX_DRIVING_LICENSE_SIZE) {
return JsonResultUtil.createFailedResult(2002,"大小超过限制!!!");
}
return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFile(file));
return JsonResultUtil.createSuccessResultWithObj(uploadService.uploadFile(file,prefix));
}
@IgnoreUserToken
......@@ -237,7 +239,10 @@ public class UploadController{
}
@RequestMapping(value = "/app/unauth/uploadFiles", method = RequestMethod.POST)
public JSONObject uploadFiles(@RequestParam("files") MultipartFile[] files)
public JSONObject uploadFiles(
@RequestParam("files") MultipartFile[] files,
@RequestParam(value = "prefix",defaultValue = "app")String prefix
)
throws Exception {
if(files!=null&&files.length>0){
String urls="";
......@@ -249,7 +254,7 @@ public class UploadController{
log.error("大小超过限制!!!");
continue;
}
String path=uploadService.uploadFile(file);
String path=uploadService.uploadFile(file,prefix);
if(StringUtils.isBlank(urls)){
urls+=path;
}else {
......
......@@ -17,6 +17,7 @@ import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Service
......@@ -35,10 +36,10 @@ public class UploadService {
* @param file
* @return
*/
public String uploadFile(MultipartFile file) throws Exception{
public String uploadFile(MultipartFile file,String prefix) throws Exception{
//创建本日存放目录
DateTime now = DateTime.now();
String dirPathToday = "/" + now.toString(DEFAULT_DATE_TIME_FORMATTER);
String dirPathToday = "/"+prefix+ "/" + now.toString(DEFAULT_DATE_TIME_FORMATTER);
String redisNoKey = RedisKey.UPLOAD_FILE_NO_PREFIX + now.toString(DEFAULT_DATE_TIME_FORMATTER);
Long no = redisTemplate.opsForValue().increment(redisNoKey);
if(no.equals(1l)){
......@@ -50,10 +51,46 @@ public class UploadService {
String filePath = baseUploadPath + realFileRelPath;
//将文件写入指定位置
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath));
realFileRelPath=xx_url+"/image/u/2/10000002/201905/o/adb766b69c0140018a3aaaa08b37d41d.jpg";
return realFileRelPath;
}
/**
* @Method: makeFileName
* @Description: 生成上传文件的文件名,文件名以:uuid+"_"+文件的原始名称
* @param filename 文件的原始名称
* @return uuid+"_"+文件的原始名称
*/
private String makeFileName(String filename){ //2.jpg
//为防止文件覆盖的现象发生,要为上传文件产生一个唯一的文件名
return UUID.randomUUID().toString() + "_" + filename;
}
/**
* 为防止一个目录下面出现太多文件,要使用hash算法打散存储
* @Method: makePath
* @Description:
*
* @param filename 文件名,要根据文件名生成存储目录
* @param savePath 文件存储路径
* @return 新的存储目录
*/
private String makePath(String filename,String savePath){
//得到文件名的hashCode的值,得到的就是filename这个字符串对象在内存中的地址
int hashcode = filename.hashCode();
int dir1 = hashcode&0xf; //0--15
int dir2 = (hashcode&0xf0)>>4; //0-15
//构造新的保存目录
String dir = savePath + "\\" + dir1 + "\\" + dir2; //upload\2\3 upload\3\5
//File既可以代表文件也可以代表目录
File file = new File(dir);
//如果目录不存在
if(!file.exists()){
//创建目录
file.mkdirs();
}
return dir;
}
/**
* 下载图片
* @param realFileRelPath
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment