目的:使用html页面上传图片到七牛云。(使用了springmvc框架)
准备工作:
1、springmvc搭建:https://blog.csdn.net/Tomwildboar/article/details/81585722
2、七牛云 对象储存创建 https://blog.csdn.net/Tomwildboar/article/details/81632859
3、springmvc上传图片:https://blog.csdn.net/Tomwildboar/article/details/81637484
4、相对应的jar包: https://download.csdn.net/download/tomwildboar/10603378
注:如果你不想这么复杂,只是想用java上传图片/文件到七牛云:
http://www.xdx97.com/#/single?bid=de501e31-1124-530d-0d93-a2c67f84393c
项目结构
index.html页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="http://localhost:8080/qiniu-upload-img/bbb.action" method="post" enctype="multipart/form-data">
<input type="file" name="img" value="请选择文件"/> <br />
<input type="submit" />
</form>
</body>
</html>
success and fail 页面
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 扫描@controler @Service 等注解 -->
<context:component-scan base-package="com.ziyang.upload.controller" />
<mvc:annotation-driven/>
<!-- 上传图片配置实现类 -->
<!-- id和class 都是固定的 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传图片的大小 -->
<property name="maxUploadSize" value="100000000000"></property>
</bean>
<!-- 视图解释器 -->
<!-- 用于配置访问地址的前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".html"/>
</bean>
</beans>
其他的配置在上面那个 springmvc入门配置已经说明了
Uploadcontroller.java
package com.ziyang.upload.controller;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
@Controller
public class UploadController {
@RequestMapping(value = "/bbb.action")
public String test1(MultipartFile img) throws IOException{
//方法一 获取相对应的流
InputStream stream = img.getInputStream();
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = "****************************************";
String secretKey = "****************************************";
String bucket = "1140459171-xiaodaoxian";
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = UUID.randomUUID().toString().replaceAll("\\-", "");
try {
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(stream,key,upToken,null, null);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
return "fail";
}
}
} catch (Exception ex) {
return "fail";
}
return "success";
}
}
测试
1、开启tomcat
2、
3、
上传成功。
后台打印结果: