使用阿里的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类

@Service
public 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类

 @Component
public 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: 2019/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";}}}

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

  1. JS中通过指定大小来压缩图片

    前不久王二写了一个图片处理库,可以指定图片质量压缩图片,调用的是javaScript的原生方法 toDataURL 和 toBlob,库里有如下这些方法: 但是通过质量压缩图片有一些不足之处:无法确定 ...

  2. 如何将图片压缩到指定大小?压缩图片大小的方法

    很多报名平台都对图片大小有固定的数值要求,所以不少小伙伴来问,怎么压缩图片200k以下(图片压缩到指定大小 图片压缩大小至指定kb以下-压缩图)?想要把图片压缩指定大小就要用到一些比较专业的工具,今天 ...

  3. 前端性能优化:使用媒体查询加载指定大小的背景图片

    日期:2013-7-8  来源:GBin1.com 直到CSS @supports被广泛支持,CSS媒体查询的使用接近于CSS中写逻辑控制.我们经常用CSS媒体查询来根据设备调整CSS属性(通常根据屏 ...

  4. Java 上传图片至OSS并返回图片地址工具类可直接用

    OSS上传图片并返回图片地址工具类 OSS上传图片并且返回地址工具类 可以直接拿去用 后续会继续更新 需要注意的点: 下面代码里的@Value里的几个值我是放在application.yml文件里的, ...

  5. java 解压缩 工具类_Java实现的zip压缩及解压缩工具类示例

    本文实例讲述了Java实现的zip压缩及解压缩工具类.分享给大家供大家参考,具体如下: import java.io.BufferedInputStream; import java.io.Buffe ...

  6. Java代码实现解压文件包和压缩文件的工具类

    最近开发任务比较多,这两天陆陆续续整理了一点资料上传一下,这个是前段时间用到的解压和压缩文件的工具类,网上找了一些,自己补充一下,现在先分享一下,希望对各位同学有所帮助! package com.as ...

  7. 【文件压缩解压工具类-含密码】

    文件压缩解压工具类-含密码 一.zip4j简介 二.zip4j工具类使用步骤 1.添加maven依赖 2.工具类代码 3.调用测试 三.结语 一.zip4j简介 zip4j功能比较强大,支持加密.解密 ...

  8. java采用降低图片分辨率大小来压缩图片大小

    我看了网上大多数对图片的压缩都是基于对图片width和hight进行裁剪来降低图片实际大小,但这种方式容易导致图片内容的丢失,所以推荐大家采用以下这种方式 import java.awt.image. ...

  9. GIF图片裁剪出指定大小的GIF图片

    前言 网购优惠券 www.fenfaw.cn 最近在博客后台上传图片的时候,突然发现上传gif图片的时候裁剪图片有问题.既没法裁剪gif指定区域的图片,又没法裁剪指定区域生成一个新的指定大小的gif图 ...

最新文章

  1. BZOJ2127happiness——最小割
  2. 刷题百道却跪在白板面试?白板面试FAQ为你答疑解惑
  3. oracle ddl会被什么阻塞,MySQL Online DDL与DML并发阻塞关系总结
  4. 将Excel文件转换为Html
  5. 【云计算】使用nsenter进入Docker容器进行调试
  6. 我国大陆居民身份证Java验证
  7. 全球及中国养老护理行业十四五趋势前景与投资动向建议报告2022版
  8. .net c# 中获得GridView的EmptyDataTemplate中的控件的方法(转)
  9. 认识事件冒泡和事件捕获
  10. c 服务器二次验证码,V5验证官网 - 基础套餐免费 -滑块验证,行为验证,防刷验证 - 文档...
  11. flux读取不到数据_初学flux的疑问?无法添加数据到DOM
  12. (软件工程复习核心重点)第十二章软件项目管理-第四节:软件配置管理和能力成熟度模型
  13. 李飞飞团队发布:中国AI期刊影响力首超美国
  14. request.getParameter、request.getParameterValues、request.getParameterMap用法
  15. Leetcode120.三角形的最小路径和 -- DP算法
  16. ipv4网络配置方法
  17. 【转】将 azw3 格式转换为 mobi 格式并保持原有排版格式
  18. PB的特点及Powerscript的语言基础
  19. 关闭安卓系统导航栏右下角自动旋转按钮
  20. 网络运营和网络营销有什么区别?

热门文章

  1. 一场美好的婚礼对女孩子来说意味着什么?
  2. dva去掉访问路径中的“#”
  3. GitLab基础:备份与恢复指南
  4. 构建乘积数组 -- 牛客网(剑指offer)
  5. 做到这一点,你也可以成为优秀的程序员(转)
  6. 家用nas装linux和windows,你会用 NAS 给 PC 装系统吗?
  7. C++实现太阳系行星系统
  8. nginx启动时指定prefix(覆盖编译时的 --prefix)
  9. 5、Cloudsim基础类
  10. 阶段式软件研发项目管理工具