JAVA_WEB批量上传文件到阿里云OS
来源:互联网 发布:植物折纸软件 编辑:程序博客网 时间:2024/05/17 13:08
JAVA_WEB批量上传文件到阿里云OS
1. 概述
从网页添加图片上传到阿里云OS服务器;
实现想法:先把文件上传到服务器所在位置,再把文件上传到阿里云OS;
以下是代码实现,主要是把图片批量上传到OS再跟进url在网页展示图片;
2. 代码实现(springmvc+spring)
2.1 Controller
package com.pro.huanbao.upimage.controller;
import org.springframework.stereotype.Controller;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class UploadImageController {
@RequestMapping(value = "/uploadImages", method = RequestMethod.POST)
private String fildUpload(
@RequestParam(value ="file", required =false) MultipartFile[]file,
HttpServletRequest request)throws Exception {
// 上传图片到本地
// 获得物理路径webapp所在路径
String pathRoot = request.getSession().getServletContext()
.getRealPath("");
String path = "";
List<String> listImagePath =new ArrayList<String>();
for (MultipartFilemf :file) {
if (!mf.isEmpty()) {
// 生成uuid作为文件名称
String uuid = UUID.randomUUID().toString().replaceAll("-","");
// 获得文件类型(可以判断如果不是图片,禁止上传)
String contentType =mf.getContentType();
// 获得文件后缀名称
String imageName =contentType.substring(contentType
.indexOf("/") + 1);
path = "/static/images/" + uuid + "." + imageName;
mf.transferTo(new File(pathRoot +path));
// 调用方法把上传到本地的图片上传到OS
String url = getImageUrl(request,path,uuid +"." +imageName);
listImagePath.add(url);
}
}
request.setAttribute("imagesPathList",listImagePath);
return"show";
}
/**
* 上传图片到阿里云OS
* @Title getImageUrl
* @param @param request
* @param @param path
* @param @return
* @return String
* @Explain
*/
private String getImageUrl(HttpServletRequestrequest, Stringpath,StringfileName) {
@SuppressWarnings("deprecation")
String realPath = request.getRealPath(path);
String endPath = realPath.replace("\\", "\\\\");
File file = new File(endPath);
// 上传本地图片到OS
String url = OssClientUtil.uploadPic(file,fileName);
// 删除本地图片
file.delete();
returnurl;
}
// 因为我的JSP在WEB-INF目录下面,浏览器无法直接访问
@RequestMapping(value = "/index")
private String forward() {
return"index";
}
}
2.2 OssUtil
package com.pro.huanbao.upimage.controller;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.net.URL;
import java.util.Date;
import com.aliyun.oss.OSSClient;
public class OssClientUtil {
private static String endpoint ="*";
private static String accessKeyId ="*";
private static String accessKeySecret ="*";
private static String bucket ="*";
private static OssClientUtil ossClientUtil;
private OssClientUtil() {
super();
}
public static OssClientUtil newInstance() {
if (ossClientUtil ==null) {
ossClientUtil =new OssClientUtil();
}
returnossClientUtil;
}
/**
* 文件流上传
* @Title uploadPic
* @param @param file
* @param @param keys
* @param @return
* @return String
* @Explain
*/
public static String uploadPic(File file, Stringkeys) {
OSSClient ossClient = new OSSClient(endpoint,accessKeyId,
accessKeySecret);//连接oss云存储服务器
ossClient.putObject(bucket,keys,file);
String url = String.valueOf(getUrl(keys));
ossClient.shutdown();
returnurl;
}
/**
* 字节数组上传
* @Title uploadPic
* @param @param content
* @param @param keys
* @param @return
* @return String
* @Explain
*/
public String uploadPic(byte[]content, Stringkeys) {
OSSClient ossClient = new OSSClient(endpoint,accessKeyId,
accessKeySecret);//连接oss云存储服务器
ossClient.putObject(bucket,keys,new ByteArrayInputStream(content));
String url = String.valueOf(getUrl(keys));
ossClient.shutdown();
returnurl;
}
/**
* 获取文件URL
* @Title getUrl
* @param @param key
* @param @return
* @return URL
*/
public static URL getUrl(String key) {
OSSClient server = new OSSClient(endpoint,accessKeyId,accessKeySecret);//连接oss云存储服务器
// 设置URL过期时间为10年 3600l* 1000*24*365*10
Date expirations = new Date(new Date().getTime() + 3600l * 1000 * 24
* 365 * 10);// url超时时间
// 生成URL
URL url = server.generatePresignedUrl(bucket,key,expirations);
server.getEndpoint();
// 关闭client
server.shutdown();
returnurl;
}
}
2.3 springmvc.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:property-placeholderlocation="classpath:resource/*.properties"/>
<context:component-scanbase-package="com.pro.huanbao.upimage.controller"/>
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix"value="/WEB-INF/jsp/"/>
<propertyname="suffix"value=".jsp"/>
</bean>
<!-- 处理对静态资源的请求 -->
<mvc:resourceslocation="/static/"mapping="/static/**"/>
<!-- 上传文件 -->
<beanid="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<propertyname="defaultEncoding"value="utf-8"/>
<!-- 最大内存大小 -->
<propertyname="maxInMemorySize"value="10240"/>
<!-- 最大文件大小,-1为不限制大小 -->
<propertyname="maxUploadSize"value="-1"/>
</bean>
<!-- 静态资源映射 -->
<mvc:resourceslocation="/WEB-INF/js/"mapping="/js/**"/>
<mvc:resourceslocation="/WEB-INF/css/"mapping="/css/**"/>
</beans>
2.4 web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"version="2.5">
<display-name>huanbao-upimage</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>cors</filter-name>
<filter-class>com.pro.huanbao.filter.MyCORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>huanbao-upimage</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>huanbao-upimage</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.5 依赖jar包
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.huanbao</groupId>
<artifactId>huanbao-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>huanbao-upimage</artifactId>
<packaging>war</packaging>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<!-- 排除依赖 -->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- 文件上传组件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- activeMq客户端 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
</dependency>
<!-- 阿里云OS -->
<dependency>
<groupId>com.aliyun.oss3</groupId>
<artifactId>aliyun-sdk-oss3</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency>
<!-- <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4</version>
</dependency> -->
<dependency>
<groupId>com.httpclient</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
</dependencies>
<!-- 配置tomcat插件 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8084</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
注:因为这个maven项目依赖父工程,所以在父工程集中定义版本了,下面是版本
2.6 jsp
2.6.1 index.jsp
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>图片上传</title>
</head>
<body>
<formaction="/uploadImages"method="post"enctype="multipart/form-data">
<label>头 像</label><inputtype="file"name="file"/><br/>
<label>头 像</label><inputtype="file"name="file"/><br/>
<label>头 像</label><inputtype="file"name="file"/><br/>
<label>头 像</label><inputtype="file"name="file"/><br/>
<label>头 像</label><inputtype="file"name="file"/><br/>
<inputtype="submit"value="提 交"/>
</form>
</body>
</html>
2.6.2 show.jsp
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglibprefix="c"uri="http://java.sun.com/jstl/core_rt"%>
<!DOCTYPEhtmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>图片展示</title>
</head>
<body>
<c:forEachitems="${imagesPathList}"var="image">
<imgsrc="${image}"><br/>
</c:forEach>
</body>
</html>
2.7 项目图谱
完整项目下载
http://download.csdn.net/download/lxinccode/10013624
- JAVA_WEB批量上传文件到阿里云OS
- 图片批量上传到阿里云服务器
- SCp上传文件到阿里云服务器
- java 上传文件到oss(阿里云)
- 上传文件到阿里云OSS
- springboot上传文件到阿里云OSS
- 使用JAVA代码实现文件上传到阿里云
- 使用javascript实现文件上传到阿里云
- java sdk 上传文件到阿里云 oos
- sftp远程上传文件到阿里云的linux服务器
- ThinkPHP 文件上传到阿里云OSS上
- 上传图片到阿里云
- 阿里云对象存储OSS--实现随时随地上传文件到阿里云
- 批量文件上传到服务器改造
- 阿里云OSS 上传文件
- 阿里云OSS 文件上传
- 阿里云上传文件,下载文件 oss
- 文件上传-阿里云OSS-存储文件
- 机器学习七:为什么要学Python
- Linux常用命令
- 腾讯的崩溃收集__________Bugly的集成
- web前端面试总结
- Mybatis关联查询
- JAVA_WEB批量上传文件到阿里云OS
- 解决disable无法传值到后台的问题
- Mysql笔记
- Spring中BeanPostProcessor
- P2P中的NAT穿越方案
- 观点 | 哈哈,TensorFlow被吐槽了吧
- Android中的自定义View(二)之 流式布局实现
- 利用博文分类提高博客访问量
- FineReport的数据决策系统注册