springboot上传excel到oss
package com.ztkj.engine.portal.controller; import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.ztkj.engine.common.result.Response;
import com.ztkj.engine.portal.entity.bean.Region;
import com.ztkj.engine.portal.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile; import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID; /**
* 版本 开发者 创建日期
* 1.0.0 InetCommunity(^_^)on 2018/12/25.
*/
@RestController
@RequestMapping("/common")
public class CommonController { @Value("${OSS.BUCKET_NAME}")
private String BUCKET_NAME;
@Value("${OSS.ENDPOINT}")
private String ENDPOINT;
@Value("${OSS.ACCESS_KEY_ID}")
private String ACCESS_KEY_ID;
@Value("${OSS.ACCESS_KEY_SECRET}")
private String ACCESS_KEY_SECRET;
@Value("${OSS.IMG_DOMAIN}")
private String IMG_DOMAIN; @Autowired
private RegionService regionService; @RequestMapping("/uploadImg")
public Response uploadImg(@RequestParam(value="file",required = false)CommonsMultipartFile file){
OSSClient ossClient=new OSSClient(ENDPOINT,ACCESS_KEY_ID,ACCESS_KEY_SECRET);
String fileType=file.getContentType();
try{
ObjectMetadata meta=new ObjectMetadata();
meta.setContentLength(file.getSize());
String fileName=file.getOriginalFilename();
String fileNewName=fileNameGenerator()+fileName.substring(fileName.lastIndexOf("."));
ossClient.putObject(BUCKET_NAME,fileNewName,file.getInputStream(),meta);
Map map=new HashMap();
map.put("filePath",fileNewName);
return Response.successResult(map);
}catch (Exception e){
return Response.dataError("上传失败,请重试");
//throw new RuntimeException("上传图片失败,请重试");
}
finally{
if(ossClient!=null){
ossClient.shutdown();
}
}
} /*
* 生成图片文件全路径
* */
public String fileNameGenerator(){
Calendar date=Calendar.getInstance();
SimpleDateFormat formatYear=new SimpleDateFormat("yyyy");
SimpleDateFormat formatMonth=new SimpleDateFormat("MM");
SimpleDateFormat formatDay=new SimpleDateFormat("dd");
String year=formatYear.format(date.getTime());
String month=formatMonth.format(date.getTime());
String day=formatDay.format(date.getTime());
String fileName= UUID.randomUUID().toString();
return year+"/"+month+"/"+day+"/"+fileName;
} @RequestMapping(value="/getRegionByPid")
public Response getRegionByPid(@RequestBody Region region){
if(region==null||region.getPid()==null){
//默认显示第一级
region=new Region();
region.setPid(0L);
}
return this.regionService.getRegionByPid(region);
}
}
springboot上传excel到oss的更多相关文章
- Springboot 上传excel并解析文件内容
最近在做一个物业的系统,需要通过excel上传业主的信息,解析并入库. 参考:https://www.cnblogs.com/jyyjava/p/8074322.html 话不多说,直接上核心代码 i ...
- SpringBoot(十三)_springboot上传Excel并读取excel中的数据
今天工作中,发现同事在整理数据,通过excel上传到数据库.所以现在写了篇利用springboot读取excel中的数据的demo.至于数据的进一步处理,大家肯定有不同的应用场景,自行修改 pom文件 ...
- postman上传excel,java后台读取excel生成到指定位置进行备份,并且把excel中的数据添加到数据库
最近要做个前端网页上传excel,数据直接添加到数据库的功能..在此写个读取excel的demo. 首先新建springboot的web项目 导包,读取excel可以用poi也可以用jxl,这里本文用 ...
- java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)
使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...
- SMW0上传EXCEL模板时报错无分配给对象***的MIME类型
在使用SMW0上传照片.声音文件.EXCEL模板等文件时,遇到报错提示,如下图所示: 解决办法:需要先维护 .XLS 文件的MIME TYPE,SMW0 打开如下图所示 选择上图红色框中“WebRFC ...
- Uploadify上传Excel到数据库
前两章简单的介绍了Uploadify上传插件的基本使用和相关的属性说明.这一章结合Uploadify+ssh框架+jquery实现Excel上传并保存到数据库. 以前写的这篇文章 Jq ...
- SpringMVc上传excel或csv文件
1.JSP页面代码 <form enctype=""multipart/form-data" method="post"> <inp ...
- 使用ocupload和POI一键上传Excel并解析导入数据库
使用的工具如下: JQuery ocupload jquery.ocupload-1.1.2.js Apache POI poi-3.9.jar 如果是Maven项目添加依赖如下: <depe ...
- java上传excel到后台解析入库
背景:最近需要做一个excel模板导入的功能,以便用户可以自己增删改查数据,当然,只有特别的用户才能有此权限,捋了捋思路,还是从前端写起 实现: 页面最后的效果如下,可以自己修改,删除,导入导出数据, ...
随机推荐
- 解决 canvas 在高清屏中绘制模糊的问题
主要代码部分: <canvas id="my_canvas" width="540" heihgt="180"></can ...
- margin/padding百分比值的计算
1.百分比介绍 一般元素的宽度用百分比值表示时,元素的总宽度包括外边距取决于父元素的width,这样可能得到"流式"页面,即元素的外边距会扩大或缩小以适应父元素的实际大小.如果对这 ...
- linux 统计代码行数
列出目录下所有文件(仅有文件名):ls -laR 列出目录下所有文件名称find . * 统计当前目录下全部代码行数find . * | xargs wc -l 统计当前目录下java文件行数,去除空 ...
- php-mbstring php7.0-zip
sudo apt install php-mbstring sudo apt install zip unzip php7.0-zip
- MySQL-创建测试数据
简单的方法(每次增加一倍): insert into tb_user(f_id, f_username) select rand(), f_username from tb_user
- Eclipse报内存溢出
(1)在配置tomcat的JDK里面设置.Window-->proference->Myeclipse-->servers-->Tomcat5-->JDK里面设置: -X ...
- 《JAVA设计模式》之模板模式(Template)
在阎宏博士的<JAVA与模式>一书中开头是这样描述模板方法(Template Method)模式的: 模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以具体方法以及具体构造函数的形式 ...
- ERROR [localhost-startStop-1] - Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/ap
ERROR [localhost-startStop-1] - Context initialization failed org.springframework.beans.factory.Bean ...
- Matplotlib基础使用
matplotlib 一.Matplotlib基础知识 Matplotlib中的基本图表包括的元素 x轴和y轴 axis 水平和垂直的轴线 x轴和y轴刻度 tick 刻度标示坐标轴的分隔,包括最小刻度 ...
- 三种分布式锁 简易说说(包含前一篇提到的redis分布式锁)
大多数互联网系统都是分布式部署的,分布式部署确实能带来性能和效率上的提升,但为此,我们就需要多解决一个分布式环境下,数据一致性的问题. 当某个资源在多系统之间,具有共享性的时候,为了保证大家访问这个资 ...