1.新增加一个文件上传接口

master
星期八 6 years ago
parent 180f894d48
commit 2d899495a6
  1. 2
      pom.xml
  2. 1
      src/main/java/com/lq/api/package-info.java
  3. 2
      src/main/java/com/lq/api/vo/UeditorVo.java
  4. 46
      src/main/java/com/lq/api/web/SysFileController.java
  5. 11
      src/main/java/com/lq/api/web/UmUeditorFileController.java
  6. 12
      src/main/java/com/lq/code/cache/RedisCache.java
  7. 19
      src/main/java/com/lq/code/util/FileUtil.java
  8. 2
      src/main/java/com/lq/entity/SysFile.java
  9. 4
      src/main/java/com/lq/wap/package-info.java
  10. 18
      src/main/java/com/lq/wap/web/WapIndexController.java

@ -17,7 +17,7 @@
<url>http://maven.apache.org</url>
<properties>
<!-- spring版本号 -->
<spring.version>4.0.2.RELEASE</spring.version>
<spring.version>5.1.1.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->

@ -1,4 +1,4 @@
package com.lq.wap.vo;
package com.lq.api.vo;
public class UeditorVo {

@ -0,0 +1,46 @@
package com.lq.api.web;
import com.lq.code.entity.AjaxResult;
import com.lq.code.util.FileUtil;
import com.lq.entity.SysFile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
@RequestMapping("/file")
public class SysFileController {
@Value("${file.upload}")
private String FILE_LOAD_PATH;
@RequestMapping("/upload")
@ResponseBody
public Object upload(@RequestParam("files")MultipartFile[] files){
AjaxResult ajaxResult = new AjaxResult();
ajaxResult.setMsg("上传成功");
Date nowTime = new Date();
SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMdd");
File fileDir = new File(FILE_LOAD_PATH+sdf.format(nowTime));
if (!fileDir.exists()){
fileDir.mkdirs();
}
for (MultipartFile multipartFile:files){
SysFile sysFile = new SysFile();
sysFile.setCreateTime(nowTime);
String fileType = FileUtil.getFileType(multipartFile.getName());
sysFile.setFileType(fileType);
sysFile.setFileName(multipartFile.getName());
}
return ajaxResult;
}
}

@ -1,7 +1,7 @@
package com.lq.wap.web;
package com.lq.api.web;
import com.lq.code.util.FileUtil;
import com.lq.wap.vo.UeditorVo;
import com.lq.api.vo.UeditorVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@ -12,11 +12,10 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
/**
@ -43,8 +42,8 @@ public class UmUeditorFileController {
fileDir.mkdirs();
}
for (MultipartFile file:files){
Random random = new Random();
Integer uuid = random.nextInt(1000000000);
ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
Integer uuid = threadLocalRandom.nextInt(1000000000);
String fileType = FileUtil.fileFormat(file.getOriginalFilename());
File file1 = new File(fileDir.getPath()+"/"+uuid.toString()+"."+fileType);
if (!file1.exists()){

@ -7,6 +7,8 @@ import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.SerializationUtils;
import java.util.concurrent.Callable;
/**
* Created by qi_liang on 2018/3/31.
*/
@ -57,6 +59,11 @@ public class RedisCache implements Cache {
return null;
}
@Override
public <T> T get(Object o, Callable<T> callable) {
return null;
}
@Override
public void put(Object key, Object value) {
final String keyf=key.toString();
@ -73,6 +80,11 @@ public class RedisCache implements Cache {
});
}
@Override
public ValueWrapper putIfAbsent(Object o, Object o1) {
return null;
}
@Override
public void evict(Object key) {
final String keyf=key.toString();

@ -20,7 +20,6 @@ public class FileUtil {
FileReader reader=new FileReader(filePath);
while (reader.ready()){
stringBuffer.append((char) reader.read());
}
reader.close();
} catch (FileNotFoundException e) {
@ -29,7 +28,6 @@ public class FileUtil {
e.printStackTrace();
}
return stringBuffer.toString();
}
@ -72,20 +70,29 @@ public class FileUtil {
* @return
*/
public static String getFileType(String fileName){
String fileType=null;
if (StringUtil.isNotNull(fileName)){
String fileFormat=fileFormat(fileName);
String fileType=null;
switch (fileFormat.toUpperCase()){
case "BMP":
case "JPEG":
case "ICO":
case "PNG":
case "JNG":
fileType= FileTypeEnum.FILE_TYPE_IMAGE.getValue();break;
fileType = FileTypeEnum.FILE_TYPE_IMAGE.getValue();break;
case "AVI":
case "MOV":
case "FLV":
case "MP4":
case "MPG":
fileType = FileTypeEnum.FILE_TYPE_VIEDO.getValue();break;
case "PDF":
fileType = FileTypeEnum.FILE_TYPE_PDF.getValue();break;
default:
fileType = FileTypeEnum.FILE_TYPE_OTHER.getValue();break;
}
}
return null;
return fileType;
}

@ -13,7 +13,7 @@ public class SysFile extends IdEntity {
private String fileName;
//文件路径(相对路径)
private String path;
//上传事件
//上传时间
private Date createTime;
//文件类型(image:图片,viedo:视频,other:其他)
private String fileType;

@ -1,4 +0,0 @@
/**
* Created by qi_liang on 2018/5/10.
*/
package com.lq.wap;

@ -1,18 +0,0 @@
package com.lq.wap.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by qi_liang on 2018/5/10.
*/
@Controller
@RequestMapping("/wap")
public class WapIndexController {
@RequestMapping("/index")
public String index(){
return "/wap/index";
}
}
Loading…
Cancel
Save