java实现多照片同时上传_多图片文件上传实现并通过 PostMan 测试

该博客介绍了在SpringCloud项目中使用Java实现多照片同时上传的详细过程。通过配置文件设置图片保存路径,Controller接收并处理上传的MultipartFile数组,利用辅助类将图片数据写入服务器指定目录,并返回上传成功的结果。同时,文章还展示了如何使用PostMan对图片上传接口进行测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目的框架是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 proPicList = new ArrayList ();

String filePath = location + imgType +"/";

try {

if(files != null && files.length >0){

for( int i=0;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; // 返回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 测试

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

d8672d5991fe5508ff597e084b3428ba.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值