Demo如下:
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的更多相关文章

  1. Springboot 上传excel并解析文件内容

    最近在做一个物业的系统,需要通过excel上传业主的信息,解析并入库. 参考:https://www.cnblogs.com/jyyjava/p/8074322.html 话不多说,直接上核心代码 i ...

  2. SpringBoot(十三)_springboot上传Excel并读取excel中的数据

    今天工作中,发现同事在整理数据,通过excel上传到数据库.所以现在写了篇利用springboot读取excel中的数据的demo.至于数据的进一步处理,大家肯定有不同的应用场景,自行修改 pom文件 ...

  3. postman上传excel,java后台读取excel生成到指定位置进行备份,并且把excel中的数据添加到数据库

    最近要做个前端网页上传excel,数据直接添加到数据库的功能..在此写个读取excel的demo. 首先新建springboot的web项目 导包,读取excel可以用poi也可以用jxl,这里本文用 ...

  4. java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)

    使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...

  5. SMW0上传EXCEL模板时报错无分配给对象***的MIME类型

    在使用SMW0上传照片.声音文件.EXCEL模板等文件时,遇到报错提示,如下图所示: 解决办法:需要先维护 .XLS 文件的MIME TYPE,SMW0 打开如下图所示 选择上图红色框中“WebRFC ...

  6. Uploadify上传Excel到数据库

    前两章简单的介绍了Uploadify上传插件的基本使用和相关的属性说明.这一章结合Uploadify+ssh框架+jquery实现Excel上传并保存到数据库.         以前写的这篇文章 Jq ...

  7. SpringMVc上传excel或csv文件

    1.JSP页面代码 <form enctype=""multipart/form-data" method="post"> <inp ...

  8. 使用ocupload和POI一键上传Excel并解析导入数据库

    使用的工具如下:  JQuery ocupload jquery.ocupload-1.1.2.js Apache POI poi-3.9.jar 如果是Maven项目添加依赖如下: <depe ...

  9. java上传excel到后台解析入库

    背景:最近需要做一个excel模板导入的功能,以便用户可以自己增删改查数据,当然,只有特别的用户才能有此权限,捋了捋思路,还是从前端写起 实现: 页面最后的效果如下,可以自己修改,删除,导入导出数据, ...

随机推荐

  1. AT3576 E Popping Balls——计数思路

    题目:https://code-festival-2017-qualb.contest.atcoder.jp/tasks/code_festival_2017_qualb_e 题解:https://w ...

  2. SpringMVC-设计模式

    MVC 设计不仅限于 Java Web 应用,还包括许多应用,比如前端.PHP..NET 等语言.之所以那么做的根本原因在于解耦各个模块. MVC 是 Model.View 和 Controller ...

  3. html、css、js分工,内核,html头,html表单

    html:内容css:样式js:交互 内核 浏览器控制台输入navigator.userAgent,回车显示出内核"Mozilla/5.0 (Windows NT 6.1; WOW64) A ...

  4. mui初级入门教程(一)— 小白入手mui的学习路线

    文章来源:小青年原创发布时间:2016-05-15关键词:mui,html5+转载需标注本文原始地址:http://zhaomenghuan.github.io/#!/blog/20160515 写在 ...

  5. curry柯里化

    Function.prototype.method = function(name,func){ if(!this.prototype[name]){ this.prototype[name] = f ...

  6. ubuntu更换源

    1.备份源 cd  /etc/apt/ sudo cp sources.list sources.list.bak 2.更换阿里源 sudo vim /etc/apt/sources.list   中 ...

  7. Chrome开发小技巧--浏览器控制台现写并运行js代码--snippets

    想简单等运行一段js代码,以前可能会新建一个html 里面包含script标签,或者引入一个js,然后chrome浏览器打开.这样很麻烦. 想再console控制台写,也不方便,换行处理麻烦. 基于在 ...

  8. Integer自动装箱和拆箱

    Integer a=3;   =>    Integer a=Integer.valueOf(3); /** *@description: 自动装箱和拆箱 *@auther: yangsj *@ ...

  9. java 集合框架 List相关接口

    AbstractCollection 此类提供 Collection 接口的骨干实现,以最大限度地减少了实现此接口所需的工作. 还有两个抽象方法,具体的迭代器,具体的Collection 的大小 pu ...

  10. vue项目运行时出现的问题(less、vue poackages version)

    今天运行项目,项目一直好好的却突然运行时报错,如下: 是引入文件报错问题,回头查看了一下文件在main.js的引入: import '@/assets/styles/custom.less'; 文件引 ...