1,oss上传信息的配置文件ossinfo.properties
AccessKeyId=
AccessKeySecret=
Endpoint=
BucketName=
AccessUrl=
2,oss配置的pojo类OSSConfigure.java
public class OSSConfigure {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
private String accessUrl;
public OSSConfigure() {
}
public OSSConfigure(String storageConfName) throws IOException {
Properties prop = new Properties();
InputStream is= super.getClass().getClassLoader().getResourceAsStream(storageConfName);
prop.load(is);
endpoint = prop.getProperty("Endpoint").trim();
accessKeyId = prop.getProperty("AccessKeyId").trim();
accessKeySecret = prop.getProperty("AccessKeySecret").trim();
bucketName = prop.getProperty("BucketName").trim();
accessUrl = prop.getProperty("AccessUrl").trim();
}
public OSSConfigure(String endpoint, String accessKeyId, String accessKeySecret, String bucketName, String accessUrl) {
this.endpoint = endpoint;
this.accessKeyId = accessKeyId;
this.accessKeySecret = accessKeySecret;
this.bucketName = bucketName;
this.accessUrl = accessUrl;
}
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getAccessKeyId() {
return accessKeyId;
}
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
}
public String getAccessKeySecret() {
return accessKeySecret;
}
public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
}
public String getBucketName() {
return bucketName;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
public String getAccessUrl() {
return accessUrl;
}
public void setAccessUrl(String accessUrl) {
this.accessUrl = accessUrl;
}
}```
#### 3,oss文件上传的工具类OSSManageUtil.java
```java
public class OSSManageUtil {
public static String uploadFile(InputStream fileContent, String remotePath, String fileName) throws Exception {
OSSConfigure ossConfigure = new OSSConfigure("config/ossinfo.properties");
OSSClient ossClient = new OSSClient(ossConfigure.getEndpoint(), ossConfigure.getAccessKeyId(), ossConfigure.getAccessKeySecret());
String remoteFilePath =remotePath+"/";
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(fileContent.available());
objectMetadata.setContentEncoding("utf-8");
objectMetadata.setCacheControl("no-cache");
objectMetadata.setHeader("Pragma", "no-cache");
objectMetadata.setContentType(contentType(fileName.substring(fileName.lastIndexOf("."))));
objectMetadata.setContentDisposition("inline;filename=" + fileName);
ossClient.putObject(ossConfigure.getBucketName(), remoteFilePath + fileName, fileContent, objectMetadata);
ossClient.shutdown();
fileContent.close();
return ossConfigure.getAccessUrl() + "/" + remoteFilePath + fileName;
}
@SuppressWarnings("unused")
public static void downloadFile(OSSConfigure ossConfigure, String key, String filename)
throws OSSException, ClientException, IOException {
OSSClient ossClient = new OSSClient(ossConfigure.getEndpoint(), ossConfigure.getAccessKeyId(),
ossConfigure.getAccessKeySecret());
OSSObject object = ossClient.getObject(ossConfigure.getBucketName(), key);
ObjectMetadata meta = object.getObjectMetadata();
InputStream objectContent = object.getObjectContent();
ObjectMetadata objectData = ossClient.getObject(new GetObjectRequest(ossConfigure.getBucketName(), key),
new File(filename));
objectContent.close();
}
public static void deleteFile( String filePath) throws IOException {
OSSConfigure ossConfigure = new OSSConfigure("/system.properties");
OSSClient ossClient = new OSSClient(ossConfigure.getEndpoint(), ossConfigure.getAccessKeyId(),
ossConfigure.getAccessKeySecret());
filePath=filePath.substring(45);
ossClient.deleteObject(ossConfigure.getBucketName(), filePath);
}
public static String contentType(String FilenameExtension) {
if (FilenameExtension.equals(".BMP") || FilenameExtension.equals(".bmp")) {
return "image/bmp";
}
if (FilenameExtension.equals(".GIF") || FilenameExtension.equals(".gif")) {
return "image/gif";
}
if (FilenameExtension.equals(".JPEG") || FilenameExtension.equals(".jpeg") || FilenameExtension.equals(".JPG")
|| FilenameExtension.equals(".jpg") || FilenameExtension.equals(".PNG")
|| FilenameExtension.equals(".png")) {
return "image/jpeg";
}
if (FilenameExtension.equals(".HTML") || FilenameExtension.equals(".html")) {
return "text/html";
}
if (FilenameExtension.equals(".TXT") || FilenameExtension.equals(".txt")) {
return "text/plain";
}
if (FilenameExtension.equals(".VSD") || FilenameExtension.equals(".vsd")) {
return "application/vnd.visio";
}
if (FilenameExtension.equals(".PPTX") || FilenameExtension.equals(".pptx") || FilenameExtension.equals(".PPT")
|| FilenameExtension.equals(".ppt")) {
return "application/vnd.ms-powerpoint";
}
if (FilenameExtension.equals(".DOCX") || FilenameExtension.equals(".docx") || FilenameExtension.equals(".DOC")
|| FilenameExtension.equals(".doc")) {
return "application/msword";
}
if (FilenameExtension.equals(".XML") || FilenameExtension.equals(".xml")) {
return "text/xml";
}
if (FilenameExtension.equals(".apk") || FilenameExtension.equals(".APK")) {
return "application/octet-stream";
}
return "text/html";
}
}
4,oss的测试方法
@RequestMapping(value = "/upload",method = RequestMethod.POST)
public String upload(@RequestParam("file") MultipartFile file){
if (file==null||"".equals(file)) {
return "上传失败";
}
try {
String fileName = file.getOriginalFilename();
String filePath = OSSManageUtil.uploadFile( file.getInputStream(), "officialwebsitefiles",fileName);
return filePath;
}catch (Exception e){
e.printStackTrace();
return "上传失败";
}
}
5,文件上传的页面upload.html
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="提交">
</form>```