2019独角兽企业重金招聘Python工程师标准>>>

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

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

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

img:location: E:\hjy\workspace\wx-back-web\src\main\webapp\olimg\

2、有关文件上传实现

.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

public class ResponseData {private Object result; // 返回结果private Object baseResult;// 基础信息返回结果private String code; // 返回codeprivate 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 测试

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

转载于:https://my.oschina.net/kuchawyz/blog/2209689

多图片文件上传实现并通过 PostMan 测试相关推荐

  1. .Net Core 图片文件上传下载

    当下.Net Core项目可是如雨后春笋一般发展起来,作为.Net大军中的一员,我热忱地拥抱了.Net Core并且积极使用其进行业务的开发,我们先介绍下.Net Core项目下实现文件上传下载接口. ...

  2. Office文档上传后实时转换为PDF格式_图片文件上传后实时裁剪_实现在线预览Office文档

    Office文档上传后实时转换为PDF格式_图片文件上传后实时裁剪 前置条件 安装LibreOffice 安装OpenOffice 安装Unoconv 安装ImageMagick.x86_64 安装G ...

  3. springmvc图片文件上传接口

    springmvc图片文件上传 用MultipartFile文件方式传输 Controller package com.controller;import java.awt.image.Buffere ...

  4. axios文件上传 formdata_基于业务场景下的图片/文件上传方案总结

    图片/文件上传组是企业项目开发中必不可少的环节之一, 但凡涉及到用户模块的都会有图片/文件上传需求, 在很多第三方组件库(ant desigin, element ui)中它也是基础组件之一. 接下来 ...

  5. php是视频还是图片格式,php 视频、音频和图片文件上传,该如何解决

    PHP code<?php ob_start(); $uploadDir = dirname(__FILE__).'/upload'; //自动创建目录 if(!file_exists($upl ...

  6. 上传绕过php文件改为图片,文件上传漏洞另类绕过技巧及挖掘案例全汇总

    文件上传漏洞作为获取服务器权限最快的方式,虽然相关资料很多,但很多人对上传校验方式.如何针对性绕过检测.哪种上传和解析的场景会产生危害等还是比较模糊.本文作一些阐述,然后补充一些除了上传webshel ...

  7. Java实现图片文件上传

    Java实现图片文件上传 Java实现后台图片上传,将上传图片的接口进行分层,便于维护接口. 接口部分(interface) 将接口分为单图片上传以及多图片上传 public interface Fi ...

  8. php中图片文件上传,显示缩略图

    php中图片文件上传,显示缩略图 htm代码块: <meta charset="utf-8" /> <style>img {max-width: 100px ...

  9. php 上传本地图片到服务器上,PHP如何将图片文件上传到另外一台服务器上

    说,我有一个需求,就是一个临时功能.由于工作开发问题,我们有一个B项目,需要有一个商品添加的功能,涉及到添加商品内容,比如商品名字,商品描述,商品库存,商品图片等.后台商品添加的接口已经写完了,但是问 ...

  10. android webview 多文件上传,Android网页WebView图片文件上传的问题

    Android网页WebView图片文件上传的问题 发布时间:2020-07-13 22:48:15 来源:51CTO 阅读:5922 作者:拾荒者老大 在安卓下,webview上传图片点击是没用的, ...

最新文章

  1. Vector 把一个vector追加到另一个vector
  2. Python mysql 索引原理与慢查询优化
  3. 二叉树和哈希表的优缺点对比与选择
  4. 总结python换源的方法:Linux与windows系统
  5. mysql视图管理和存储过程
  6. java Future FutureTask 并发操作
  7. Web开发中实用小工具
  8. Drchen的学习清单
  9. WARNING:tensorflow:Entity <bound method GRUCell.call of <tensorflow.python.ops.rnn_cell_impl.GRUCell
  10. Java Applet
  11. 个人学习笔记附Markdown格式下载
  12. html点击按钮状态改变,html点击按钮改变文字怎么实现
  13. mysql的安装配置
  14. Ubuntu18.04搭建本地RTMP服务器librtmp+nginx,推送flv文件播放
  15. 五行通天地 八卦定乾坤--打算按照先天八卦的形制重构Lite模版引擎的指令集...
  16. 怎么关闭计算机安全,电脑中安全防护怎么关闭
  17. QT The inferior stopped because it received a signal from the operating system.
  18. CENTOS上的网络安全工具(十二)走向Hadoop(4) Hadoop 集群搭建
  19. java 两集合根据某字段 过滤掉相同的数据 , 集合整合数据
  20. 拒绝室友抄计算机作业,大学关系很好的室友抄你的作业,如何拒绝?

热门文章

  1. ReentrantLock 源码解析
  2. 浏览器href自动解析uri(执行urldecode)
  3. mysql 历史记录查询
  4. Delphi中Format与FormatDateTime函数详解
  5. C#中 As 和强制转换的总结
  6. bwlabel和bwconncomp区别
  7. macOS安装过程中“应用副本已损坏”的解决方案
  8. Arturia Buchla Easel V for Mac(Buchla音乐画架插件)
  9. Acrobat Pro DC 2022 Mac(全能PDF工具)完美兼容m1
  10. Java基础之时间的友好显示