Java中使用RestFul接口上传图片到阿里云OSS服务器
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>