项目的框架是springCloud 的一个服务,图片上传实际上就是用java的IO 流进行读写文件,给后台传递一个路径,后台经过浏览器连接到前端,将前端的图片数据拷贝的服务器的存储地址。前端
1. 服务端保存图片的地址:java
application.yml 文件中添加以下配置:web
img: location: E:\hjy\workspace\wx-back-web\src\main\webapp\olimg\
二、有关文件上传实现spring
.Controller 中获取路径的浏览器
import org.springframework.beans.factory.annotation.Value; @Value("${img.location}") private String location;
Controller 中的方法:服务器
import org.springframework.web.bind.annotation.PathVariable; @RequestMapping("/uploadImg/{imgType}") public String uploadImg( @RequestParam("ediormd-image-file") MultipartFile[] files,@PathVariable("imgType") String imgType) { ResponseData res = new ResponseData(); List<ProductPic> proPicList = new ArrayList<ProductPic> (); String filePath = location + imgType +"/"; try { if(files != null && files.length >0){ for( int i=0;i<files.length;i++){ ProductPic proPic = new ProductPic(); MultipartFile file = files[i]; String contentType = file.getContentType(); String fileName = file.getOriginalFilename(); System.out.println("fileName-->" + fileName); System.out.println("getContentType-->" + contentType); String resfileNewName = FileUtil.uploadFile(file.getBytes(), filePath, fileName); proPic.setPicUrl(filePath +resfileNewName); if(imgType.equals("list")){ proPic.setPicName("列表图"); }else if("caro".equals(imgType)){ proPic.setPicName("轮播图"); }else if("intr".equals(imgType)){ proPic.setPicName("介绍图"); }else if("thum".equals(imgType)){ proPic.setPicName("缩略图"); } proPicList.add(proPic); } } res.setCode(Constant.SUCCESS_CODE); res.setResult(proPicList); res.setMessage(Constant.SUCCESS_MSG); } catch (IOException e) { e.printStackTrace(); res.setCode(Constant.FAIL_CODE); res.setMessage(Constant.FAIL_MSG); } catch (Exception e) { e.printStackTrace(); res.setCode(Constant.FAIL_CODE); res.setMessage(Constant.FAIL_MSG); } JSONObject rdJson = JSONObject.fromObject(res); return rdJson.toString(); }
辅助类ResponseData app
public class ResponseData { private Object result; // 返回结果 private Object baseResult;// 基础信息返回结果 private String code; // 返回code private String message; // 返回提醒消息 public Object getResult() { return result; } public void setResult(Object result) { this.result = result; } public Object getBaseResult() { return baseResult; } public void setBaseResult(Object baseResult) { this.baseResult = baseResult; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
文件上传服务类:框架
public class FileUtil { /** * 上传文件并返回文件的名字 * @param file * @param filePath * @param fileName * @return * @throws Exception */ public static String uploadFile(byte[] file,String filePath,String fileName) throws Exception{ String time = DateHelper.formatDate(new Date(), "yyMMddHHssmm"); fileName = time +"_" +fileName ; File targetFile = new File(filePath); if(!targetFile.exists()){ targetFile.mkdir(); } FileOutputStream out = new FileOutputStream(filePath + fileName); out.write(file); out.flush(); out.close(); return fileName; } }
3. 经过PostMan 测试webapp
安装以下图红色标注的,就能够实现对图片上传的测试,测试