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. Queue1循环队列

    循环队列 1 #include<iostream> using namespace std; //#define maxSize 20 template <class T> c ...

  2. 攻防世界 | string

    #encoding=utf-8 #!usr/bin/python from pwn import * io = remote('111.198.29.45',42643) io.recvuntil(& ...

  3. xpath定位不到原因浅析

    在爬虫中,我们经常使用xpath来对元素进行定位,xpath定位分为两种,一种是绝对定位,/html/body/div[2]/div[1]/div/div[3]/a[7],另外一种是相对定位,比如r' ...

  4. django缓存优化(二)

    一.缓存目的: 1.减小过载 2.避免重复计算 3.提高系统性能 二.如何进行缓存 三.缓存类型 四.缓存粒度分类 五.缓存的设置与使用 示例一: CACHES = { 'default': { 'B ...

  5. IBatis.Net 下使用SqlBulkCopy 大批量导入数据 问题解决

    SQLBulkCopy是继承SQLClient空间下的一个特殊类,它可以帮助我们以映射的方式把DataTable和DataReader数据大批量导入到数据库对应表中 public void Inert ...

  6. What code you will get when you create a wcf library

    创建wcf服务库的时候,系统自动生成的代码 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”. [ServiceContract] publ ...

  7. 136、TensorFlow的Embedding lookup

    import tensorflow as tf; import numpy as np; c = np.random.random([10, 1]) b = tf.nn.embedding_looku ...

  8. 爬虫(十一)—— XPath总结

    目录 XPath总结 一.何为XPath 二.XPath语法 1.语法 2.实例 三.XPath轴 1.XPath轴语法 2.XPath轴实例 四.XPath运算符 XPath总结 一.何为XPath ...

  9. IDF-CTF-cookie欺骗 writeup

    题目链接: http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=40 知识点:base64解码, cookie欺骗 ...

  10. java.lang.NumberFormatException: For input string: "title"异常

    java.lang.NumberFormatException: For input string: "title" at java.lang.NumberFormatExcept ...