700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 上传图片到OSS 并按指定大小范围压缩图片。完整工具类+三层调用

上传图片到OSS 并按指定大小范围压缩图片。完整工具类+三层调用

时间:2019-02-03 12:09:55

相关推荐

上传图片到OSS 并按指定大小范围压缩图片。完整工具类+三层调用

使用阿里的Oss服务器,做图片服务器。 项目功能为,前端,调用后端的接口,上传用户的图片!

1、controller类

@RestController@RequestMapping("/oss/file")public class FileController {private static final Logger logger = LoggerFactory.getLogger(FileController.class);@Autowiredprivate IFileService service;/*** 上传文件到临时库,并使用 上传文件的原文件名* @param file* @return*/@PostMapping(value = "/uploadFileUsingOriginalFilename")public ResultRsp uploadFileUsingOriginalFilename(MultipartFile file){return ResultRsp.success(service.uploadFileUsingOriginalFilename(file),"文件上传成功");}/*** 上传文件到临时库,使用随机生成的文件名* @param file* @return*/@PostMapping(value = "/uploadFile")public Object uploadFile(MultipartFile file){return ResultRsp.success(service.uploadFile(file),"文件上传成功");}}

2、service类

@Servicepublic class IFileServiceImpl implements IFileService {private final static Logger LOGGER = LoggerFactory.getLogger(IFileServiceImpl.class);private final static String FILE_URL = "fileUrl";/*** 上传文件到临时库,并使用 上传文件的原文件名* @param file* @return* @throws IOException*/@Overridepublic Map uploadFileUsingOriginalFilename(MultipartFile file) throws IOException {this.checkFile(file);String name = OssClientUtil.uploadFileTemp(file, file.getOriginalFilename());String fileUrl = OssClientUtil.getFileUrlFromTemp(name,false);return MapUtils.toMap(FILE_URL,fileUrl);}/*** 上传文件到临时库,并使用随机名* @param file* @return* @throws IOException*/@Overridepublic Map uploadFile(MultipartFile file) throws IOException {this.checkFile(file);String name = OssClientUtil.uploadFileTemp(file);String fileUrl = OssClientUtil.getFileUrlFromTemp(name,false);return MapUtils.toMap(FILE_URL,fileUrl);}/*** 检查 文件是否为null* @param file*/private void checkFile(MultipartFile file){if (Objects.isNull(file) || Objects.isNull(file.getOriginalFilename())){throw new SecurityException(ErrorCode.Null.OBJECT_NULL,"文件对象为null");}LOGGER.debug("上传文件:filename=" + file.getOriginalFilename());}public class MapUtils {public static Map toMap(String key, Object value){HashMap map = new HashMap(1);map.put(key,value);return map;}}}

3、OssClientUtil类

@Componentpublic class OssClientUtil {private static final Logger LOGGER = LoggerFactory.getLogger(OssClientUtil.class);@Autowiredprivate OssConfig ossConfig;private static OSSClient ossClient;private static OSSClient ossClientInternal;private static UidGenerator uidGenerator;private static OssClientUtil ossClientUtil;private static String baseDir = "data/";/*** 文件正式存储目录*/private static String fileDir = baseDir + "file/";/*** 文件临时存储目录*/private static String tempDir = baseDir + "temp/";private static boolean intranet;/*** 初始化*/@PostConstructpublic void init() {ossClientUtil = this;LOGGER.debug("OSS Util 加载的配置数据为{}", ossClientUtil.ossConfig.toString());ossClient = new OSSClient(ossClientUtil.ossConfig.getEndpoint(), ossClientUtil.ossConfig.getAccessKeyId(), ossClientUtil.ossConfig.getAccessKeySecret());ossClientInternal = new OSSClient(ossClientUtil.ossConfig.getEndpointInternal(), ossClientUtil.ossConfig.getAccessKeyId(), ossClientUtil.ossConfig.getAccessKeySecret());if (ossClientUtil.ossConfig.getIntranet()) {intranet = true;} else {intranet = false;}uidGenerator = new SnowflakeUidGenerator();}/*** 销毁*/public static void destory() {ossClient.shutdown();ossClientInternal.shutdown();}/*** 上传文件到临时库,并使用指定文件名,限定大小10M** @param file文件流* @param fileName 自定义文件名,包含后缀名* @return 返回时间码拼接的文件名,目的是为了避免重复。格式为 "时间串_随机码_fileName"*/public static String uploadFileTemp(MultipartFile file, String fileName) throws MyFileException, IOException {OssUtil.checkFileSizeGt(file, OssUtil.M_10);String name = System.currentTimeMillis() + "_" + uidGenerator.nextId() + "_" + fileName;InputStream inputStream;try {inputStream = file.getInputStream();} catch (Exception e) {throw new MyFileException(ErrorCode.File.FILE_NOT_FOUND, "文件上传失败");}uploadFileTemp(inputStream, name);return name;}/*** 上传文件到OSS服务器 的临时目录下, 如果同名文件会覆盖服务器上的文件** @param inputStream 文件流* @param fileName 文件名称 包括后缀名* @return 出错返回"" ,唯一MD5数字签名* @throws MyFileException* @throws IOException*/public static String uploadFileTemp(InputStream inputStream, String fileName) throws MyFileException, IOException {return uploadFileOSSData(inputStream, ossClientUtil.ossConfig.getTempData(), fileName, tempDir);}/*** 上传到OSS服务器 指定目录下** @param inputStream 文件流* @param bucketName bucket名* @param fileName 新的文件名称* @param dir 上传至目标目录下* @return 文件上传成功后的,MD5码* @throws MyFileException* @throws IOException*/public static String uploadFileOSSData(InputStream inputStream, String bucketName, String fileName, String dir) throws MyFileException, IOException {//压缩图片inputStream = changeSize(inputStream,ossClientUtil.ossConfig.getSize());OSSClient ossClient;if (OssClientUtil.intranet) {ossClient = OssClientUtil.ossClientInternal;} else {ossClient = OssClientUtil.ossClient;}String ret = "";try {//创建上传Object的MetadataObjectMetadata objectMetadata = new ObjectMetadata();objectMetadata.setContentLength(inputStream.available());objectMetadata.setCacheControl("no-cache");objectMetadata.setHeader("Pragma", "no-cache");objectMetadata.setContentType(OssUtil.getContentType(fileName.substring(fileName.lastIndexOf("."))));objectMetadata.setContentDisposition("inline;filename=" + fileName);//设置 公共读objectMetadata.setObjectAcl(CannedAccessControlList.PublicRead);//上传文件PutObjectResult putResult = ossClient.putObject(bucketName, dir + fileName, inputStream, objectMetadata);ret = putResult.getETag();} catch (RuntimeException e) {LOGGER.error("上传文件到OSS服务器 发生错误:{}", e.getMessage());throw new MyFileException(ErrorCode.File.FILE_OSS_ERROR, e.getMessage(), e.getLocalizedMessage());} finally {if (inputStream != null) {inputStream.close();}}return ret;}/*** 递归 压缩文件流,到指定大小范围内* @param inputStream* @param limitSize* @return* @throws IOException*/public static InputStream changeSize(InputStream inputStream,int limitSize) throws IOException {//先判断 文件大小是否超过 规定大小。if (! (inputStream.available()>limitSize)){return inputStream;}// 压缩比率为 0.8float ratio = 0.8f;BufferedImage bufImage = ImageIO.read(inputStream);ByteArrayOutputStream bos = new ByteArrayOutputStream();//写入 临时文件File file = new File(OssClientUtil.uidGenerator.nextId()+".png");Thumbnails.of(bufImage).scale(ratio).toFile(file);//获取输出流inputStream = new FileInputStream(file);//删除临时文件file.delete();LOGGER.info("转换后的文件大小为{}",inputStream.available());//递归调用return changeSize(inputStream,limitSize);}}

4、OssUtil类

/*** @Author: seowen* @Date: /10/25 14:52* @Version 1.0*/public class OssUtil {public static final long KB_512 = 512*1024;public static final long M_5 = 5*1024*1024;public static final long M_10 = 10*1024*1024;public static final long G_1 = 1*1024*1024*1024;public static final long Year = 1*365*24*3600L*1000;/*** 验证 文件大小,不能大于 限定大小* @param file* @param limitSize 字节*/public static void checkFileSizeGt(MultipartFile file,long limitSize){if (file.getSize() > limitSize) {throw new MyFileException(ErrorCode.File.FILE_SIZE_BIG,"上传文件大小不能大于:"+limitSize);}}/*** 验证 文件大小,不能小于 限定大小* @param file* @param limitSize 字节*/public static void checkFileSizeLt(MultipartFile file,long limitSize){if (file.getSize() < limitSize) {throw new MyFileException(ErrorCode.File.FILE_SIZE_SMALL,"上传文件大小不能小于:"+limitSize);}}/*** 验证 文件大小,只能在 限定大小之间* @param file* @param ltLimitSize* @param gtLimitSize*/public static void checkFileSizeBetween(MultipartFile file,long ltLimitSize,long gtLimitSize){if (file.getSize() < ltLimitSize || file.getSize() > gtLimitSize) {throw new MyFileException(ErrorCode.File.FILE_SIZE_ERROR,"上传文件大小超出限定范围,范围为:"+ltLimitSize+" 和 "+gtLimitSize);}}/*** Description: 判断OSS服务文件上传时文件的contentType** @param fileNameExtension 文件后缀* @return String*/public static String getContentType(String fileNameExtension) {switch(fileNameExtension) {case ".": return "application/x-" ;case ".*": return "application/octet-stream" ;case ".pdf": return "application/pdf" ;case ".ai": return "application/postscript" ;case ".edi": return "application/EDIFACT" ;case ".json": return "application/json" ;case ".js": return "application/javascript" ;case ".ogg": return "application/ogg" ;case ".rdf": return "application/rdf+xml" ;case ".woff": return "application/font-woff" ;case ".xhtml": return "application/xhtml+xml" ;case ".xml": return "application/xml" ;case ".dtd": return "application/xml-dtd" ;case ".zip": return "application/zip" ;case ".gzip": return "application/gzip" ;case "0.001": return "application/x-001" ;case "0.301": return "application/x-301" ;case "0.906": return "application/x-906" ;case ".a11": return "application/x-a11" ;case ".awf": return "application/vnd.adobe.workflow" ;case ".bmp": return "application/x-bmp" ;case ".c4t": return "application/x-c4t" ;case ".cal": return "application/x-cals" ;case ".cdf": return "application/x-netcdf" ;case ".cel": return "application/x-cel" ;case ".cg4": return "application/x-g4" ;case ".cit": return "application/x-cit" ;case ".bot": return "application/x-bot" ;case ".c90": return "application/x-c90" ;case ".cat": return "application/vnd.ms-pki.seccat" ;case ".cdr": return "application/x-cdr" ;case ".cer": return "application/x-x509-ca-cert" ;case ".cgm": return "application/x-cgm" ;case ".cmx": return "application/x-cmx" ;case ".crl": return "application/pkix-crl" ;case ".csi": return "application/x-csi" ;case ".cut": return "application/x-cut" ;case ".dbm": return "application/x-dbm" ;case ".cmp": return "application/x-cmp" ;case ".cot": return "application/x-cot" ;case ".crt": return "application/x-x509-ca-cert" ;case ".dbf": return "application/x-dbf" ;case ".dbx": return "application/x-dbx" ;case ".dcx": return "application/x-dcx" ;case ".dgn": return "application/x-dgn" ;case ".dll": return "application/x-msdownload" ;case ".dot": return "application/msword" ;case ".der": return "application/x-x509-ca-cert" ;case ".dib": return "application/x-dib" ;case ".doc": return "application/msword" ;case ".drw": return "application/x-drw" ;case ".dwf": return "application/x-dwf" ;case ".dxb": return "application/x-dxb" ;case ".edn": return "application/vnd.adobe.edn" ;case ".dwg": return "application/x-dwg" ;case ".dxf": return "application/x-dxf" ;case ".emf": return "application/x-emf" ;case ".epi": return "application/x-epi" ;case ".eps": return "application/postscript" ;case ".exe": return "application/x-msdownload" ;case ".fdf": return "application/vnd.fdf" ;case ".etd": return "application/x-ebx" ;case ".fif": return "application/fractals" ;case ".frm": return "application/x-frm" ;case ".gbr": return "application/x-gbr" ;case ".g4": return "application/x-g4" ;case ".gl2": return "application/x-gl2" ;case ".hgl": return "application/x-hgl" ;case ".hpg": return "application/x-hpgl" ;case ".hqx": return "application/mac-binhex40" ;case ".hta": return "application/hta" ;case ".gp4": return "application/x-gp4" ;case ".hmr": return "application/x-hmr" ;case ".hpl": return "application/x-hpl" ;case ".hrf": return "application/x-hrf" ;case ".icb": return "application/x-icb" ;case ".ico": return "application/x-ico" ;case ".ig4": return "application/x-g4" ;case ".iii": return "application/x-iphone" ;case ".ins": return "application/x-internet-signup" ;case ".iff": return "application/x-iff" ;case ".igs": return "application/x-igs" ;case ".img": return "application/x-img" ;case ".isp": return "application/x-internet-signup" ;case ".jpe": return "application/x-jpe" ;case ".jpg": return "application/x-jpg" ;case ".lar": return "application/x-laplayer-reg" ;case ".latex": return "application/x-latex" ;case ".lbm": return "application/x-lbm" ;case ".ls": return "application/x-javascript" ;case ".ltr": return "application/x-ltr" ;case ".man": return "application/x-troff-man" ;case ".mdb": return "application/msaccess" ;case ".mac": return "application/x-mac" ;case ".mp3": return "audio/mp3";case ".mp4": return "video/mpeg4";case ".mfp": return "application/x-shockwave-flash" ;case ".mi": return "application/x-mi" ;case ".mil": return "application/x-mil" ;case ".mocha": return "application/x-javascript" ;case ".mpd": return "application/vnd.ms-project" ;case ".mpp": return "application/vnd.ms-project" ;case ".mpt": return "application/vnd.ms-project" ;case ".mpw": return "application/vnd.ms-project" ;case ".mpx": return "application/vnd.ms-project" ;case ".mxp": return "application/x-mmxp" ;case ".nrf": return "application/x-nrf" ;case ".out": return "application/x-out" ;case ".p12": return "application/x-pkcs12" ;case ".p7c": return "application/pkcs7-mime" ;case ".p7r": return "application/x-pkcs7-certreqresp" ;case ".pc5": return "application/x-pc5" ;case ".pcl": return "application/x-pcl" ;case ".pdx": return "application/vnd.adobe.pdx" ;case ".pgl": return "application/x-pgl" ;case ".pko": return "application/vnd.ms-pki.pko" ;case ".p10": return "application/pkcs10" ;case ".p7b": return "application/x-pkcs7-certificates" ;case ".p7m": return "application/pkcs7-mime" ;case ".p7s": return "application/pkcs7-signature" ;case ".pci": return "application/x-pci" ;case ".pcx": return "application/x-pcx" ;case ".pfx": return "application/x-pkcs12" ;case ".pic": return "application/x-pic" ;case ".pl": return "application/x-perl" ;case ".plt": return "application/x-plt" ;case ".png": return "application/x-png" ;case ".ppa": return "application/vnd.ms-powerpoint" ;case ".pps": return "application/vnd.ms-powerpoint" ;case ".ppt": return "application/x-ppt" ;case ".prf": return "application/pics-rules" ;case ".prt": return "application/x-prt" ;case ".ps": return "application/postscript" ;case ".pwz": return "application/vnd.ms-powerpoint" ;case ".ra": return "audio/vnd.rn-realaudio" ;case ".ras": return "application/x-ras" ;case ".pot": return "application/vnd.ms-powerpoint" ;case ".ppm": return "application/x-ppm" ;case ".pr": return "application/x-pr" ;case ".prn": return "application/x-prn" ;case ".ptn": return "application/x-ptn" ;case ".red": return "application/x-red" ;case ".rjs": return "application/vnd.rn-realsystem-rjs" ;case ".rlc": return "application/x-rlc" ;case ".rm": return "application/vnd.rn-realmedia" ;case ".rat": return "application/rat-file" ;case ".rec": return "application/vnd.rn-recording" ;case ".rgb": return "application/x-rgb" ;case ".rjt": return "application/vnd.rn-realsystem-rjt" ;case ".rle": return "application/x-rle" ;case ".rmf": return "application/vnd.adobe.rmf" ;case ".rmj": return "application/vnd.rn-realsystem-rmj" ;case ".rmp": return "application/vnd.rn-rn_music_package" ;case ".rmvb": return "application/vnd.rn-realmedia-vbr" ;case ".rnx": return "application/vnd.rn-realplayer" ;case ".rpm": return "audio/x-pn-realaudio-plugin" ;case ".rms": return "application/vnd.rn-realmedia-secure" ;case ".rmx": return "application/vnd.rn-realsystem-rmx" ;case ".rsml": return "application/vnd.rn-rsml" ;case ".rtf": return "application/msword" ;case ".rv": return "video/vnd.rn-realvideo" ;case ".sat": return "application/x-sat" ;case ".sdw": return "application/x-sdw" ;case ".slb": return "application/x-slb" ;case ".sam": return "application/x-sam" ;case ".sdp": return "application/sdp" ;case ".sit": return "application/x-stuffit" ;case ".sld": return "application/x-sld" ;case ".smi": return "application/smil" ;case ".smk": return "application/x-smk" ;case ".smil": return "application/smil" ;case ".spc": return "application/x-pkcs7-certificates" ;case ".spl": return "application/futuresplash" ;case ".ssm": return "application/streamingmedia" ;case ".stl": return "application/vnd.ms-pki.stl" ;case ".sst": return "application/vnd.ms-pki.certstore" ;case ".tdf": return "application/x-tdf" ;case ".tga": return "application/x-tga" ;case ".sty": return "application/x-sty" ;case ".swf": return "application/x-shockwave-flash" ;case ".tg4": return "application/x-tg4" ;case ".tif": return "application/x-tif" ;case ".vdx": return "application/vnd.visio" ;case ".vpg": return "application/x-vpeg005" ;case ".vst": return "application/vnd.visio" ;case ".vsw": return "application/vnd.visio" ;case ".vtx": return "application/vnd.visio" ;case ".torrent": return "application/x-bittorrent" ;case ".vda": return "application/x-vda" ;case ".vsd": return "application/vnd.visio" ;case ".vss": return "application/vnd.visio" ;case ".vsx": return "application/vnd.visio" ;case ".wb1": return "application/x-wb1" ;case ".wb3": return "application/x-wb3" ;case ".wiz": return "application/msword" ;case ".wk4": return "application/x-wk4" ;case ".wks": return "application/x-wks" ;case ".wb2": return "application/x-wb2" ;case ".wk3": return "application/x-wk3" ;case ".wkq": return "application/x-wkq" ;case ".wmf": return "application/x-wmf" ;case ".wmd": return "application/x-ms-wmd" ;case ".wp6": return "application/x-wp6" ;case ".wpg": return "application/x-wpg" ;case ".wq1": return "application/x-wq1" ;case ".wri": return "application/x-wri" ;case ".ws": return "application/x-ws" ;case ".wmz": return "application/x-ms-wmz" ;case ".wpd": return "application/x-wpd" ;case ".wpl": return "application/vnd.ms-wpl" ;case ".wr1": return "application/x-wr1" ;case ".wrk": return "application/x-wrk" ;case ".ws2": return "application/x-ws" ;case ".xdp": return "application/vnd.adobe.xdp" ;case ".xfd": return "application/vnd.adobe.xfd" ;case ".xfdf": return "application/vnd.adobe.xfdf" ;case ".xls": return "application/vnd.ms-excel" ;case ".xwd": return "application/x-xwd" ;case ".sis": return "application/vnd.symbian.install" ;case ".x_t": return "application/x-x_t" ;case ".apk": return "application/vnd.android.package-archive" ;case ".x_b": return "application/x-x_b" ;case ".sisx": return "application/vnd.symbian.install" ;case ".ipa": return "application/vnd.iphone" ;case ".xap": return "application/x-silverlight-app" ;case ".xlw": return "application/x-xlw" ;case ".xpl": return "audio/scpls" ;case ".anv": return "application/x-anv" ;case ".uin": return "application/x-icq" ;case ".001": return " application/x-001";case ".301": return " application/x-301";case ".323": return " text/h323";case ".906": return " application/x-906";case ".907": return " drawing/907";case ".acp": return " audio/x-mei-aac";case ".aif": return " audio/aiff";case ".aifc": return " audio/aiff";case ".aiff": return " audio/aiff";case ".asa": return " text/asa";case ".asf": return " video/x-ms-asf";case ".asp": return " text/asp";case ".asx": return " video/x-ms-asf";case ".au": return " audio/basic";case ".avi": return " video/avi";case ".biz": return " text/xml";case ".class": return " java/*";case ".cml": return " text/xml";case ".css": return " text/css";case ".dcd": return " text/xml";case ".eml": return " message/rfc822";case ".ent": return " text/xml";case ".fax": return " image/fax";case ".fo": return " text/xml";case ".gcd": return " application/x-gcd";case ".gif": return " image/gif";case ".htc": return " text/x-component";case ".htm": return " text/html";case ".html": return " text/html";case ".htt": return " text/webviewhtml";case ".htx": return " text/html";case ".IVF": return " video/x-ivf";case ".java": return " java/*";case ".jfif": return " image/jpeg";case ".jpeg": return " image/jpeg";case ".jsp": return " text/html";case ".la1": return " audio/x-liquid-file";case ".lavs": return " audio/x-liquid-secure";case ".lmsff": return " audio/x-la-lms";case ".m1v": return " video/x-mpeg";case ".m2v": return " video/x-mpeg";case ".m3u": return " audio/mpegurl";case ".m4e": return " video/mpeg4";case ".math": return " text/xml";case ".mht": return " message/rfc822";case ".mhtml": return " message/rfc822";case ".mid": return " audio/mid";case ".midi": return " audio/mid";case ".mml": return " text/xml";case ".mnd": return " audio/x-musicnet-download";case ".mns": return " audio/x-musicnet-stream";case ".movie": return " video/x-sgi-movie";case ".mp1": return " audio/mp1";case ".mp2": return " audio/mp2";case ".mp2v": return " video/mpeg";case ".mpa": return " video/x-mpg";case ".mpe": return " video/x-mpeg";case ".mpeg": return " video/mpg";case ".mpg": return " video/mpg";case ".mpga": return " audio/rn-mpeg";case ".mps": return " video/x-mpeg";case ".mpv": return " video/mpg";case ".mpv2": return " video/mpeg";case ".mtx": return " text/xml";case ".net": return " image/pnetvue";case ".nws": return " message/rfc822";case ".odc": return " text/x-ms-odc";case ".plg": return " text/html";case ".pls": return " audio/scpls";case ".r3t": return " text/vnd.rn-realtext3d";case ".ram": return " audio/x-pn-realaudio";case ".rmi": return " audio/mid";case ".rmm": return " audio/x-pn-realaudio";case ".rp": return " image/vnd.rn-realpix";case ".rt": return " text/vnd.rn-realtext";case ".slk": return " drawing/x-slk";case ".snd": return " audio/basic";case ".sol": return " text/plain";case ".sor": return " text/plain";case ".spp": return " text/xml";case ".stm": return " text/html";case ".svg": return " text/xml";case ".tiff": return " image/tiff";case ".tld": return " text/xml";case ".top": return " drawing/x-top";case ".tsd": return " text/xml";case ".txt": return " text/plain";case ".uls": return " text/iuls";case ".vcf": return " text/x-vcard";case ".vml": return " text/xml";case ".vxml": return " text/xml";case ".wav": return " audio/wav";case ".wax": return " audio/x-ms-wax";case ".wbmp": return " image/vnd.wap.wbmp";case ".wm": return " video/x-ms-wm";case ".wma": return " audio/x-ms-wma";case ".wml": return " text/vnd.wap.wml";case ".wmv": return " video/x-ms-wmv";case ".wmx": return " video/x-ms-wmx";case ".wsc": return " text/scriptlet";case ".wsdl": return " text/xml";case ".wvx": return " video/x-ms-wvx";case ".xdr": return " text/xml";case ".xq": return " text/xml";case ".xql": return " text/xml";case ".xquery": return " text/xml";case ".xsd": return " text/xml";case ".xsl": return " text/xml";case ".xslt": return " text/xml";default: return "image/jpeg";}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。