首先需要注册一个七牛云账号:七牛云 - 国内领先的企业级云服务商 (qiniu.com)
这里我选择的地区是华东地区。
首先:pom.xml
<!--七牛云-->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.8.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
upload.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="file"/> <br/> <input type="submit" value="提交"/>
</form>
</body>
</html>
service:
uploadservice
package com.yzx.boot.service;
import java.io.InputStream;
public interface UploadService {
void upload(InputStream inputStream, String fileName);
}
uploadServiceimpl:
package com.yzx.boot.service;
import com.google.gson.Gson;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Service;
import java.io.InputStream;
@Service
public class UploadServiceimpl implements UploadService{
@Override
public void upload(InputStream inputStream, String fileName) {
try {
//构造一个带指定 Region 对象的配置类
//region0华东
Configuration cfg = new Configuration(Region.region0());
//...其他参数参考类注释
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
//密钥
String accessKey = "jArzWGjZeFXuOJhItw9KBMfCwXqm_r7cLpNsS1kQ";
String secretKey = "yyXRdKg4YfV5NB--3wAN2eYi0wpg1XE14dN7tQQe";
//空间名字
String bucket = "yangp1";
//默认不指定key的情况下,以文件内容的hash值作为文件名
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
Response response = uploadManager.put(inputStream, fileName, upToken,null,null);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println(ex.toString());
}
}
}
controller:
package com.yzx.boot.controller;
import com.yzx.boot.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
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.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@RestController
public class UploController {
@Autowired
private UploadService uploadService;
@RequestMapping("/upload")
@ResponseBody
public String upload(@RequestParam(name = "file") MultipartFile file) throws IOException {
uploadService.upload(file.getInputStream(),file.getOriginalFilename());
//如果不为空返回成功,为空返回失败
if(uploadService!=null){
return "success";
}else{
return "error";
}
}
}
最后建一个限制文件上传大小的类:
MyConfig
package com.yzx.boot.config;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import javax.servlet.MultipartConfigElement;
@Configuration
public class MyConfig {
// 设置上传文件限制
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory config = new MultipartConfigFactory();
//单个文件大小 KB、MB必须是大写
config.setMaxFileSize(DataSize.parse("100MB"));
//设置总文件大小
config.setMaxRequestSize(DataSize.parse("1000MB"));
return config.createMultipartConfig();
}
}
最后成型照片: