package com.haier.controller.newuser;

import com.haier.commons.entity.Response;
import com.haier.commons.utils.AliyunOSSUtil; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File;
import java.util.UUID;
/**
* 文件操作Controller.
*
* @author Will Tong
*
*/
@Controller
@RequestMapping("/file")
public class FileController {
private Logger log = Logger.getLogger(getClass());
private static long PICMAXSIZE = 2097152;
private static long FILEMAXSIZE= 10485760;//定义文件上传的最大大小为5M
@RequestMapping(value ="/uploadfile",produces="text/plain;charset=UTF-8")
@ResponseBody
public String uploadFile(HttpServletRequest request, @RequestParam(required = false) MultipartFile streamFile) {
Response<String> response=new Response<>();
String fileName = streamFile.getOriginalFilename();
if (!StringUtils.isEmpty(fileName)) {
try { if (streamFile.getSize() <= FILEMAXSIZE) {
// String reg = ".+(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png)$";
// Pattern pattern = Pattern.compile(reg);
// Matcher matcher = pattern.matcher(streamFile.getOriginalFilename().toLowerCase());
UUID uuid = UUID.randomUUID();
String resultFileName = uuid.toString() + "." + this.getExtensionName(fileName);
File file = new File(request.getSession().getServletContext().getRealPath("/") + "temp/", resultFileName);
FileUtils.copyInputStreamToFile(streamFile.getInputStream(), file);
String uri = AliyunOSSUtil.PutObject(AliyunOSSUtil.ClassificationEnum.MECV,AliyunOSSUtil.FolderEnum.ORDER , resultFileName, file);
// 上传后删除
file.delete();
String url = "http://" + AliyunOSSUtil.GetBucket() + "." + AliyunOSSUtil.GetDomain() + "/" + uri;
response.setResult(url);
} else {
response.setError("图片大小超过5M,请重新选择!");
} } catch (Exception e) {
log.error(String.format("图片上传失败失败"));
response.setError("图片上传失败失败!");
}
}else{
response.setError("图片名字为空无法上传!");
}
return response.toJson();
} public String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
}

spring mvc文件上传的更多相关文章

  1. Spring MVC 笔记 —— Spring MVC 文件上传

    文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...

  2. Spring MVC文件上传教程 commons-io/commons-uploadfile

    Spring MVC文件上传教程 commons-io/commons-uploadfile 用到的依赖jar包: commons-fileupload 1.3.1 commons-io 2.4 基于 ...

  3. 【Java Web开发学习】Spring MVC文件上传

    [Java Web开发学习]Spring MVC文件上传 转载:https://www.cnblogs.com/yangchongxing/p/9290489.html 文件上传有两种实现方式,都比较 ...

  4. Spring mvc文件上传实现

    Spring mvc文件上传实现 jsp页面客户端表单编写 三个要素: 1.表单项type="file" 2.表单的提交方式:post 3.表单的enctype属性是多部分表单形式 ...

  5. Spring mvc 文件上传到文件夹(转载+心得)

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  6. spring mvc 文件上传 ajax 异步上传

    异常代码: 1.the request doesn't contain a multipart/form-data or multipart/mixed stream, content type he ...

  7. spring mvc文件上传(单个文件上传|多个文件上传)

    单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包    1.所需jar包:                commons-fileupload-1.3.1.jar       ...

  8. Strut2 和Spring MVC 文件上传对比

    在Java领域中,有两个常用的文件上传项目:一个是Apache组织Jakarta的Common-FileUpload组件 (http://commons.apache.org/proper/commo ...

  9. Spring MVC 文件上传 & 文件下载

    索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadCon ...

  10. 【Spring】Spring MVC文件上传--整合bootstrap-fileinput和jQuery-File-Upload

    前言 这里分享两个使用Spring MVC进行文件上传的简单示例, 分别整合bootstrap-fileinput 和 Jquery File Upload , 代码十分简单, 都是入门的示例,因此这 ...

随机推荐

  1. DOS命令追加符的使用

    @echo off start \\192.168.10.120\常用软件\系统工具\远程客户端\winvnc.exe #打开共享的远程客户端程序 ipconfig /all > d:\disp ...

  2. 使用SpringMVC集成SpringSession的问题

    最近在使用SpringSession时遇到一个问题,错误日志如下: Exception sending context initialized event to listener instance o ...

  3. iOS - UIButton设置文字标题下划线以及下划线颜色

    创建button设置可以折行显示 - (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [[UIButton alloc] in ...

  4. c语言简易版文法

    文法 <程序>→<外部声明>|<程序><外部声明> <外部声明>→<函数定义>|<声明> <函数定义>→ ...

  5. HTML5探秘:用requestAnimationFrame优化Web动画

    本文转载自: HTML5探秘:用requestAnimationFrame优化Web动画

  6. paper 115:常见的概率分布(matlab作图)

    一.常见的概率分布 表1.1 概率分布分类表 连续随机变量分布 连续统计量分布 离散随机变量分布 分布 分布 二项分布 连续均匀分布 非中心 分布 离散均匀分布 (Gamma)分布 分布 几何分布 指 ...

  7. centos安装mono

    1.查看mono最新版本 http://download.mono-project.com/sources/mono 2.安装依赖环境 sudo yum install cmake automake ...

  8. ios异常错误

    1,mach_msg_trap处异常 http://www.jianshu.com/p/2b3f58c61d7d 在lldb下敲入bt (lldb) bt

  9. Objective-C基础2

    16.继承 1)不能和父类定义一样的变量 2)单一继承,不支持继承多个类 3)支持多层继承子类也可以被继承 17.实例变量修饰符 public:公开的,在其他类中也可以访问 protected:受保护 ...

  10. nes 红白机模拟器 第3篇 游戏手柄测试 51 STM32

    手柄使用的是 CD4021 ,datasheet 上说支持 3V - 15V . 因为手柄是 5V 供电,2440 开发板上是GPIO 3.3V 电平,STM32 GPIO 也是 3.3V (也兼容5 ...