1.接口方法
import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.alibaba.fastjson.JSONObject; import com.voyage.client.util.ApiResponse; import com.voyage.client.util.OSSUtil; import com.voyage.client.util.StringUtil; @RestController @RequestMapping("/resource") public class BasicController { /* * 圖片上傳 */ @PostMapping("/imgUpload") public ApiResponse imgUpload(HttpServletRequest request, @RequestParam("file") MultipartFile file) { String url = ""; if (file != null) { String fileName = file.getOriginalFilename();//獲取上傳原圖片名稱 String newFileName = StringUtil.guid() + fileName.substring(fileName.lastIndexOf("."));//生成保存在服務器的圖片名稱,延用原后綴名 try { OSSUtil.upload(newFileName, file.getInputStream()); url = newFileName; } catch (IOException e) { e.printStackTrace(); } } JSONObject r = new JSONObject(); r.put("url", OSSUtil.getUrl(url, "")); //r.put("snapshotUrl", OSSUtil.getUrl(url, "?x-oss-process=image/resize,m_fixed,h_200,w_200")); return new ApiResponse(r); } }
2.oss圖片上傳工具類
import com.aliyun.oss.OSSClient; import com.aliyun.oss.model.ObjectMetadata; import com.aliyun.oss.model.PutObjectResult; import java.io.IOException; import java.io.InputStream; import java.net.URL; public class OSSUtil { private static String endpoint = "oss-cn-shenzhen.aliyuncs.com"; private static String accessKeyId = "LT*******3"; private static String accessKeySecret = "U**************Y4A"; private static String bucketName = "bucketName"; private static OSSClient ossClientStatic; static { ossClientStatic = new OSSClient(endpoint, accessKeyId, accessKeySecret); } /** * 上傳到OSS服務器 如果同名文件會覆蓋服務器上的 * * @param fileName 文件名稱 包括后綴名 * @param instream 文件流 * @return 出錯返回"" ,唯一MD5數字簽名 */ public static String upload(String fileName, InputStream instream) { String resultStr = ""; try { // 創建上傳Object的Metadata ObjectMetadata objectMetadata = new ObjectMetadata(); // objectMetadata.setContentLength(instream.available()); // objectMetadata.setCacheControl("no-cache"); // objectMetadata.setHeader("Pragma", "no-cache"); // objectMetadata.setContentType(getContentType(fileName.substring(fileName.lastIndexOf(".")))); // objectMetadata.setContentDisposition("inline;filename=" + fileName); // 上傳文件 (上傳文件流的形式) PutObjectResult putResult = ossClientStatic.putObject(bucketName, fileName, instream, objectMetadata); // 解析結果 resultStr = putResult.getETag(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (instream != null) { instream.close(); } } catch (IOException e) { e.printStackTrace(); } } return resultStr; } /** * 上傳圖片 * * @param url */ public static void uploadUrl(String fileName, String url) { try { InputStream instream = new URL(url).openStream(); upload(fileName, instream); } catch (Exception e) { e.printStackTrace(); } finally { } } /** * 獲得url鏈接 * * @param key * @return */ public static String getUrl(String key, String option) { if (StringUtil.isBlank(key)) return ""; return "http://" + bucketName + "." + endpoint + "/" + key + option; } public static String getBaseUrl() { return "http://" + bucketName + "." + endpoint + "/"; } }
3.maven需要引入的阿里雲oss服務器jar包
<dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>2.4.0</version> </dependency>
