51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2576|回复: 1
打印 上一主题 下一主题

[原创] 多图片文件上传实现并通过 PostMan 测试

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-1-28 14:45:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

项目的框架是springCloud 的一个服务,图片上传实际上就是用java的IO 流进行读写文件,给后台传递一个路径,后台通过浏览器链接到前端,将前端的图片数据拷贝的服务器的存储地址。

1. 服务端保存图片的地址:

application.yml 文件中添加如下配置:

  1. img:
  2.   location: E:\hjy\workspace\wx-back-web\src\main\webapp\olimg\
复制代码

2、有关文件上传实现

.Controller 中获取路径的

  1. import org.springframework.beans.factory.annotation.Value;


  2.   @Value("${img.location}")
  3.         private String location;
复制代码

  Controller 中的方法:

  1. import org.springframework.web.bind.annotation.PathVariable;


  2. @RequestMapping("/uploadImg/{imgType}")
  3.         public String uploadImg(
  4.                         @RequestParam("ediormd-image-file") MultipartFile[] files,@PathVariable("imgType") String imgType) {
  5.                 ResponseData res = new ResponseData();
  6.          List<ProductPic> proPicList = new ArrayList<ProductPic> ();
  7.         String filePath = location + imgType +"/";
  8.         try {
  9.                 if(files != null && files.length >0){
  10.                         for( int i=0;i<files.length;i++){
  11.                                 ProductPic proPic = new ProductPic();
  12.                                 MultipartFile file = files[i];
  13.                                 String contentType = file.getContentType();
  14.                                 String fileName = file.getOriginalFilename();
  15.                                 System.out.println("fileName-->" + fileName);
  16.                                 System.out.println("getContentType-->" + contentType);
  17.                                 String resfileNewName = FileUtil.uploadFile(file.getBytes(), filePath, fileName);
  18.                                 proPic.setPicUrl(filePath +resfileNewName);
  19.                                 if(imgType.equals("list")){
  20.                                         proPic.setPicName("列表图");
  21.                                 }else if("caro".equals(imgType)){
  22.                                         proPic.setPicName("轮播图");
  23.                                 }else if("intr".equals(imgType)){
  24.                                         proPic.setPicName("介绍图");
  25.                                 }else if("thum".equals(imgType)){
  26.                                         proPic.setPicName("缩略图");
  27.                                 }
  28.                                 proPicList.add(proPic);
  29.                         }
  30.                 }
  31.                         res.setCode(Constant.SUCCESS_CODE);
  32.                         res.setResult(proPicList);
  33.                         res.setMessage(Constant.SUCCESS_MSG);
  34.                 } catch (IOException e) {
  35.                         e.printStackTrace();
  36.                         res.setCode(Constant.FAIL_CODE);
  37.                         res.setMessage(Constant.FAIL_MSG);
  38.                 } catch (Exception e) {
  39.                         e.printStackTrace();
  40.                         res.setCode(Constant.FAIL_CODE);
  41.                         res.setMessage(Constant.FAIL_MSG);
  42.                 }
  43.                 JSONObject rdJson = JSONObject.fromObject(res);
  44.                 return rdJson.toString();
  45.         }
复制代码

辅助类ResponseData

  1. public class ResponseData {
  2.         private Object result; // 返回结果
  3.         private Object baseResult;// 基础信息返回结果
  4.         private String code; // 返回code
  5.         private String message; // 返回提醒消息

  6.         public Object getResult() {
  7.                 return result;
  8.         }

  9.         public void setResult(Object result) {
  10.                 this.result = result;
  11.         }

  12.         public Object getBaseResult() {
  13.                 return baseResult;
  14.         }

  15.         public void setBaseResult(Object baseResult) {
  16.                 this.baseResult = baseResult;
  17.         }

  18.         public String getCode() {
  19.                 return code;
  20.         }

  21.         public void setCode(String code) {
  22.                 this.code = code;
  23.         }

  24.         public String getMessage() {
  25.                 return message;
  26.         }

  27.         public void setMessage(String message) {
  28.                 this.message = message;
  29.         }

  30. }
复制代码

文件上传服务类:

  1. public class FileUtil {

  2.         /**
  3.          * 上传文件并返回文件的名字
  4.          * @param file
  5.          * @param filePath
  6.          * @param fileName
  7.          * @return
  8.          * @throws Exception
  9.          */
  10.         public static String uploadFile(byte[] file,String filePath,String fileName) throws Exception{
  11.                 String time = DateHelper.formatDate(new Date(), "yyMMddHHssmm");
  12.         fileName = time +"_" +fileName ;
  13.                 File targetFile = new File(filePath);
  14.                 if(!targetFile.exists()){
  15.                         targetFile.mkdir();
  16.                 }
  17.                
  18.                 FileOutputStream out = new FileOutputStream(filePath + fileName);
  19.                 out.write(file);
  20.                 out.flush();
  21.                 out.close();
  22.                
  23.                 return fileName;
  24.         }
  25. }
复制代码

3. 通过PostMan 测试

  安装如下图红色标注的,就可以实现对图片上传的测试,


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2019-5-18 09:46:25 | 只看该作者
后台接收多张图片的时候长度总是为0,一张图片没问题。
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-9 08:20 , Processed in 0.067032 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表