springboot中怎么实现单文件和多文件上传-创新互联

本篇文章给大家分享的是有关springboot中怎么实现单文件和多文件上传,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业成都托管服务器报价,主机托管价格性价比高,为金融证券行业服务器托管,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。

具体内容如下

package com.heeexy.example.controller;import com.alibaba.fastjson.JSONObject;import com.heeexy.example.util.CommonUtil;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import javax.servlet.http.HttpServletRequest;import java.io.*;import java.util.*;@RestController@RequestMapping("/common")public class UploadController { //设置上传文件夹 File uploadPath = null; //单文件上传 @PostMapping("/upload") public JSONObject upload(@RequestParam(value = "file", required = false)MultipartFile file,HttpServletRequest request) throws Exception{  //定义返回客户端json对象  JSONObject returnData = new JSONObject();  //定义处理流对象  BufferedOutputStream out = null;  //将request对象转成JSONObject对象  JSONObject jsonObject = CommonUtil.request2Json(request);  //验证必填字段  CommonUtil.hasAllRequired(jsonObject,"user_id,equi_id,upload_type");  //获取当前用户id  String user_id = jsonObject.getString("user_id");  //获取当前设备id  String equi_id = jsonObject.getString("equi_id");  //获取上传文件的类型 1:巡检 2:维保  String upload_type = jsonObject.getString("upload_type");  //判断上传文件类型并设置前置路径  File uploadPath = null;  String basePath = "/root/img";     //基础文件上传路径  String inspection = "/inspection";    //巡检文件夹路径  String maintenance = "/maintenance";   //维保文件夹路径  switch (upload_type){   case "1":    uploadPath = new File(basePath+inspection);    break;   case "2":    uploadPath = new File(basePath+maintenance);    break;   default:    uploadPath = new File(basePath);  }  //判断服务器上传文件夹是否存在  if(!uploadPath.exists()){   uploadPath.mkdirs();  }  //判断上传的文件是否为空  if (file!=null) {   //获取上传文件后缀   String houzhui = file.getOriginalFilename().split("\\.")[1];   //拼接上传文件保存路径(当前用户id+设备id+时间戳.后缀名)   File fil = new File(uploadPath+"/"+user_id+equi_id+new Date().getTime()+"."+houzhui);   try {    //将上传文件保存到服务器上传文件夹目录下    out = new BufferedOutputStream(new FileOutputStream(fil));    out.write(file.getBytes());    out.flush();    out.close();    //返回上传文件的访问路径 getAbsolutePath()返回文件上传的绝对路径    returnData.put("message",fil.getName());   } catch (FileNotFoundException e) {    e.printStackTrace();    returnData.put("message", "文件上传失败:" + e.getMessage());   } catch (IOException e) {    e.printStackTrace();    returnData.put("message", "文件上传失败:" + e.getMessage());   }finally {    //关闭处理流    if(out!=null){out.close();}   }  } else {   returnData.put("message", "文件上传失败,文件为空");  }  return CommonUtil.successJson(returnData); } //多文件上传 @PostMapping("/batchUpload") public JSONObject handleFileUpload(HttpServletRequest request) throws Exception{  //定义返回客户端json对象  JSONObject returnData = new JSONObject();  //定义处理流对象,处理文件上传  BufferedOutputStream stream = null;  //定义map存储返回结果集  Map returnfileMap = new HashMap();  //获取前端上传的文件列表  List files = ((MultipartHttpServletRequest) request).getFiles("file");  MultipartFile file = null;  //将request对象转成JSONObject对象  JSONObject jsonObject = CommonUtil.request2Json(request);  //验证必填字段,用户id,设备id,上传文件类型  CommonUtil.hasAllRequired(jsonObject,"user_id,equi_id,upload_type");  //获取当前用户id  String user_id = jsonObject.getString("user_id");  //获取当前设备id  String equi_id = jsonObject.getString("equi_id");  //获取上传文件的类型 1:巡检 2:维保  String upload_type = jsonObject.getString("upload_type");  //判断上传文件类型并设置前置路径  File uploadPath = null;  String basePath = "/root/img"; //基础文件上传路径  String inspection = "/inspection"; //巡检文件夹路径  String maintenance = "/maintenance"; //维保文件夹路径  switch (upload_type){   case "1":    uploadPath = new File(basePath+inspection);    break;   case "2":    uploadPath = new File(basePath+maintenance);    break;   default:    uploadPath = new File(basePath);  }  //判断服务器上传文件夹是否存在  if(!uploadPath.exists()){   uploadPath.mkdirs();  }  //遍历客户端上传文件列表  for (int i = 0; i < files.size(); ++i) {   //获取到每个文件   file = files.get(i);    try {     //获取上传文件后缀     String houzhui = file.getOriginalFilename().split("\\.")[1];     //拼接上传文件保存在服务器的路径(当前用户id+设备id+时间戳.后缀名)     File fil = new File(uploadPath+"/"+user_id+equi_id+new Date().getTime()+"."+houzhui);     //将上传文件保存到服务器上传文件夹目录下     byte[] bytes = file.getBytes();     stream = new BufferedOutputStream(new FileOutputStream(fil));     stream.write(bytes);     stream.close();     //每成功上传一个文件,将上传文件名作为key,服务器保存路径作为value存入returnfileMap中     switch (upload_type){      case "1":       returnfileMap.put(file.getOriginalFilename(),inspection+"/"+fil.getName());       break;      case "2":       returnfileMap.put(file.getOriginalFilename(),maintenance+"/"+fil.getName());       break;     }    } catch (Exception e) {     stream = null;     //保存上传失败的文件信息,将上传文件名作为key,value值为"fail",存入returnfileMap中     returnfileMap.put(file.getOriginalFilename(),"fail");    }finally {     //关闭处理流     if(stream!=null){stream.close();}    }  }  //返回returnfileMap集合到客户端  returnData.put("message",returnfileMap);  return CommonUtil.successJson(returnData);  } }

以上就是springboot中怎么实现单文件和多文件上传,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


本文题目:springboot中怎么实现单文件和多文件上传-创新互联
标题URL:http://pwwzsj.com/article/dpeohh.html