我里面涉及到的静态类和返回类: 静态类(FilePath) package com.ds.common;/** 文件上传下载路径* */public class FilePath { //文件存放路径 public static String PATH="D://DSfile"; //用户头像上传路径 publ
我里面涉及到的静态类和返回类: 静态类(FilePath)
package com.ds.common;/** 文件上传下载路径* */public class FilePath { //文件存放路径 public static String PATH="D://DSfile"; //用户头像上传路径 public static String CZYB_HEAD_PATH="//user";}返回类(RespBean):
package com.ds.entity;/** 公共返回对象* */public class RespBean { private long code; private String message; private Object obj; public RespBean() { } public RespBean(long code, String message, Object obj) { this.code = code; this.message = message; this.obj = obj; } public long getCode() { return code; } public void setCode(long code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Object getObj() { return obj; } public void setObj(Object obj) { this.obj = obj; } @Override public String toString() { return "RespBean{" + "code=" + code + ", message='" + message + '\'' + ", obj=" + obj + '}'; } /* * 成功返回结果 * */ public static RespBean sucess(String message){ return new RespBean(200,message,null); } /* * 成功返回结果 * */ public static RespBean sucess(String message,Object obj){ return new RespBean(200,message,obj); } /* * 失败返回结果 * */ public static RespBean error(String message){ return new RespBean(500,message,null); } /* * 失败返回结果 * */ public static RespBean error(String message,Object obj){ return new RespBean(500,message,obj); }}controller类
package com.ds.controller;import com.ds.common.FilePath;import com.ds.entity.RespBean;import com.ds.service.FileService;import com.ds.utils.StringUtil;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;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.File;import java.util.HashMap;import java.util.List;import java.util.Map;@RestControllerpublic class FileController { @Autowired FileService fileService; @RequestMapping("/fileUpload") public RespBean fileUpload(@RequestParam("file") MultipartFile file, String bs){ if(bs.equals("userHead")){//用户头像上传路径 Map<String,String> resultMap= this.saveFile(file,FilePath.CZYB_HEAD_PATH); return RespBean.sucess(resultMap.get("msg"),resultMap.get("fileId")); }else{ return RespBean.error("上传失败,请联系官网人员"); } } private Map<String,String> saveFile(MultipartFile file, String path){ Map<String,String> resultMap = new HashMap<>(); if (file.isEmpty()){ resultMap.put("msg","上传文件为空,请重新上传!"); return resultMap; } //上传服务器路径 String filePath = FilePath.PATH + path; //文件名 String fileName = file.getOriginalFilename(); //最后下载或者显示文件名称 String showname = fileName.substring(0,fileName.lastIndexOf(".")); //获取文件的后缀名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); //重新命名,解决中文乱码等问题 String lastFileName = StringUtil.getStringUUID32()+suffixName; //实例化这个文件 File dsFile = new File(filePath+lastFileName); //第一次运行,本地或者服务器没有目录,需要先创建目录 if(!dsFile.getParentFile().exists()){ dsFile.getParentFile().mkdirs(); } try { file.transferTo(dsFile); com.ds.entity.File saveFile = new com.ds.entity.File(path,lastFileName,showname); fileService.creatFile(saveFile); resultMap.put("msg","上传成功!"); resultMap.put("fileId",saveFile.getId()); return resultMap; }catch (Exception e){ e.printStackTrace(); } resultMap.put("msg","上传失败!"); return resultMap; } @RequestMapping("/fileUploads") public RespBean fileUploads(HttpServletRequest request,String bs){ List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file"); if(bs.equals("userHead")){//用户头像上传路径 Map<String,String> resultMap= this.saveFiles(files,FilePath.CZYB_HEAD_PATH); return RespBean.sucess(resultMap.get("msg"),resultMap.get("fileId")); }else{ return RespBean.error("上传失败,请联系官网人员"); } } private Map<String,String> saveFiles(List<MultipartFile> files,String path){ Map<String,String> resultMap = new HashMap<>(); com.ds.entity.File saveFile = null; String beiz = StringUtil.getStringUUID32(); boolean judy = false; for(MultipartFile file : files){ //上传服务器路径 String filePath = FilePath.PATH + path; //文件名 String fileName = file.getOriginalFilename(); //最后下载或者显示文件名称 String showname = fileName.substring(0,fileName.lastIndexOf(".")); //获取文件的后缀名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); //重新命名,解决中文乱码等问题 String lastFileName = StringUtil.getStringUUID32()+suffixName; //实例化这个文件 File dsFile = new File(filePath+lastFileName); //第一次运行,本地或者服务器没有目录,需要先创建目录 if(!dsFile.getParentFile().exists()){ dsFile.getParentFile().mkdirs(); } try { file.transferTo(dsFile); saveFile = new com.ds.entity.File(path,lastFileName,showname); saveFile.setBeiz(beiz); fileService.creatFile(saveFile); judy = true; }catch (Exception e){ judy = false; e.printStackTrace(); } } if(judy){ resultMap.put("msg","上传成功!"); resultMap.put("fileId",beiz); }else{ resultMap.put("msg","上传失败!"); } return resultMap; }}