1.接口方法

1import java.io.IOException; 2 3import javax.servlet.http.HttpServletRequest; 4 5import org.springframework.web.bind.annotation.PostMapping; 6import org.springframework.web.bind.annotation.RequestMapping; 7import org.springframework.web.bind.annotation.RequestParam; 8import org.springframework.web.bind.annotation.RestController; 9import org.springframework.web.multipart.MultipartFile; 10 11import com.alibaba.fastjson.JSONObject; 12import com.voyage.client.util.ApiResponse; 13import com.voyage.client.util.OSSUtil; 14import com.voyage.client.util.StringUtil; 15 16@RestController 17@RequestMapping("/resource") 18public class BasicController { 19 20 /* 21 * 图片上传 22 */ 23 @PostMapping("/imgUpload") 24 public ApiResponse imgUpload(HttpServletRequest request, @RequestParam("file") MultipartFile file) { 25 26 String url = ""; 27 if (file != null) { 28 29 String fileName = file.getOriginalFilename();//获取上传原图片名称 30 String newFileName = StringUtil.guid() + fileName.substring(fileName.lastIndexOf("."));//生成保存在服务器的图片名称,延用原后缀名 31 try { 32 OSSUtil.upload(newFileName, file.getInputStream()); 33 url = newFileName; 34 } catch (IOException e) { 35 36 e.printStackTrace(); 37 } 38 } 39 JSONObject r = new JSONObject(); 40 r.put("url", OSSUtil.getUrl(url, "")); 41 //r.put("snapshotUrl", OSSUtil.getUrl(url, "?x-oss-process=image/resize,m_fixed,h_200,w_200")); 42 return new ApiResponse(r); 43 44 } 45 46 47}
View Code
2.oss图片上传工具类

1import com.aliyun.oss.OSSClient; 2import com.aliyun.oss.model.ObjectMetadata; 3import com.aliyun.oss.model.PutObjectResult; 4 5import java.io.IOException; 6import java.io.InputStream; 7import java.net.URL; 8 9public class OSSUtil { 10 11 private static String endpoint = "oss-cn-shenzhen.aliyuncs.com"; 12 13 private static String accessKeyId = "LT*******3"; 14 15 private static String accessKeySecret = "U**************Y4A"; 16 17 private static String bucketName = "bucketName"; 18 19 private static OSSClient ossClientStatic; 20 21 static { 22 ossClientStatic = new OSSClient(endpoint, accessKeyId, accessKeySecret); 23 } 24 25 /** 26 * 上传到OSS服务器 如果同名文件会覆盖服务器上的 27 * 28 * @param fileName 文件名称 包括后缀名 29 * @param instream 文件流 30 * @return 出错返回"" ,唯一MD5数字签名 31 */ 32 public static String upload(String fileName, InputStream instream) { 33 String resultStr = ""; 34 try { 35 36 // 创建上传Object的Metadata 37 ObjectMetadata objectMetadata = new ObjectMetadata(); 38// objectMetadata.setContentLength(instream.available()); 39// objectMetadata.setCacheControl("no-cache"); 40// objectMetadata.setHeader("Pragma", "no-cache"); 41// objectMetadata.setContentType(getContentType(fileName.substring(fileName.lastIndexOf(".")))); 42// objectMetadata.setContentDisposition("inline;filename=" + fileName); 43 44 // 上传文件 (上传文件流的形式) 45 PutObjectResult putResult = ossClientStatic.putObject(bucketName, fileName, instream, objectMetadata); 46 // 解析结果 47 resultStr = putResult.getETag(); 48 } catch (Exception e) { 49 e.printStackTrace(); 50 51 } finally { 52 try { 53 if (instream != null) { 54 instream.close(); 55 } 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 } 60 return resultStr; 61 62 } 63 64 /** 65 * 上传图片 66 * 67 * @param url 68 */ 69 public static void uploadUrl(String fileName, String url) { 70 71 try { 72 InputStream instream = new URL(url).openStream(); 73 upload(fileName, instream); 74 } catch (Exception e) { 75 e.printStackTrace(); 76 77 } finally { 78 79 } 80 } 81 82 /** 83 * 获得url链接 84 * 85 * @param key 86 * @return 87 */ 88 public static String getUrl(String key, String option) { 89 if (StringUtil.isBlank(key)) 90 return ""; 91 return "http://" + bucketName + "." + endpoint + "/" + key + option; 92 93 } 94 95 public static String getBaseUrl() { 96 97 return "http://" + bucketName + "." + endpoint + "/"; 98 } 99}
View Code
3.maven需要引入的阿里云oss服务器jar包
1<dependency> 2 <groupId>com.aliyun.oss</groupId> 3 <artifactId>aliyun-sdk-oss</artifactId> 4 <version>2.4.0</version> 5 </dependency>