问个问题,上传到阿里云的图片是清晰的,去阿里云里看也是清晰的。但是从阿里云那儿下载到本地服务器,图片就有点模糊。是什么原因。大神请赐教
你用什么下载?
不好意思,我没说清楚。是这样的,我们后台程序员生成链接,然后前端根据链接加载出图片,然后显示出来的图片看着就很模糊。在阿里云上看是清晰的
@zhuzhukangkang: 你把在啊里云里面的图片大小和显示出来的图片大小比对一下,看是不是被压缩了。
@盟怀部孩:
http://mmdimage.oss-cn-hangzhou.aliyuncs.com/201612201510288091717_1242x2208_0.5625?Expires=1482224773&OSSAccessKeyId=TMP.AQGQzBiSkEI72VeMtqUiCeUvVLOPxrzCSkCCEAEzrbnvHdZYaWRsbeey-wWxADAtAhR0y32NDxEjh2PYuuMHoKIgNqrPpwIVAJjqQyqqCAYwziBCUZrm8sS7L6lj&Signature=1DNRjnBiTkT%2F9vaMZljsbrjbyXk%3D这个是阿里云的图片
https://imgs.manmanda.cn/201612201510288091717_1242x2208_0.5625%40%21index2?Expires=1483199940&OSSAccessKeyId=Q8bYUwi7QQ8qngvN&Signature=VBIFoEIDrCWzsbdQD7hZOvbV8N0%3D这个是后台生成的链接的图片
@zhuzhukangkang: 被压缩了。
让后台程序员处理。
@盟怀部孩: 是后台把图片压缩了对吧
@zhuzhukangkang: 是的。
@盟怀部孩: 但是后台说用的是阿里云提供的方法,是无压缩的方法,怎么搞
@zhuzhukangkang: 因为后台给的图片,大小只是原图的32%
@盟怀部孩:
package cn.manmanda.api.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import cn.manmanda.api.controllers.Global;
import cn.manmanda.core.util.CommonUtil;
import cn.manmanda.core.util.StringUtil;
/**
* 阿里OSS工具类
* <li>主要生成带签名的URL,借APP端下载访问
*
* @Title: OSSUtil.java
* @Package cn.manmanda.api.util
* @Description: TODO(用一句话描述该文件做什么)
* @author JannySao(ksgameboy@qq.com)
* @date 2015-4-10 下午11:07:44
* @version V1.0
*/
public class OSSFileUtil {
private static final String ACCESS_ID = "Q8bYUwi7QQ8qngvN";
private static final String ACCESS_KEY = "QELBBFH4D5H41muptC92DLhpowU8Rr";
private static OSSClient client = new OSSClient(ACCESS_ID, ACCESS_KEY);
/**
* 图片端 private static OSSClient client = new OSSClient(ACCESS_ID, ACCESS_KEY);
*/
private static OSSClient imgclient = new OSSClient("http://imgs.manmanda.cn",ACCESS_ID, ACCESS_KEY);
/**
* 生成签名URL
* @param urlfile
*/
public static String getUrl(String bucket, String urlfile){
//h = hmac.new("OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV",
//"GET\n\n\n1141889120\n/oss-example/oss-api.pdf",
//sha)
if(StringUtil.isEmpty(urlfile) || StringUtil.isEmpty(bucket)){
return "";
}
//远程连接地址,直接返回
if(urlfile.indexOf("http://")>=0 || urlfile.indexOf("https://")>=0){
return urlfile;
}
Date exp = CommonUtil.getMonthLastDate();
URL url = client.generatePresignedUrl(bucket, urlfile, exp, HttpMethod.GET);
//System.out.println(url.toString());
System.out.println(url.toString().replaceFirst("http", "https"));
return url.toString().replaceFirst("http", "https");
}
/**
* 生成带签名的缩略图
* @param bucket
* @param urlfile
* @return
*/
public static String getThumbnail(String bucket, String urlfile, String styleName){
if(StringUtil.isEmpty(urlfile) || StringUtil.isEmpty(bucket)){
return "";
}
//远程连接地址,直接返回
if(urlfile.indexOf("http://")>=0 || urlfile.indexOf("https://")>=0){
return urlfile;
}
URL url = null;
Date exp = CommonUtil.getMonthLastDate();
if("mmdvideo".equals(bucket)){
url = client.generatePresignedUrl(bucket, urlfile, exp, HttpMethod.GET);
}else{
urlfile += "@!"+styleName;
url = imgclient.generatePresignedUrl(bucket, urlfile, exp, HttpMethod.GET);
}
//System.out.println(url.toString());
return url.toString().replaceFirst("http", "https");
}
/**
* 将字符串资源转换为List资源
* @param urls
* @return
*/
public static List<String> transform2List(String urls, String bucket){
List<String> urllist = new ArrayList<String>();
if(!StringUtil.isEmpty(urls)){
String[] words = urls.split("\\s+|[,,]");
for(String word : words){
if(!StringUtil.isEmpty(word)){
urllist.add(OSSFileUtil.getThumbnail(bucket, word, Global.OSSIMAGE_INDEX2));
}
}
}
return urllist;
}
/**
* 作品转换为缩略图
* @param urls
* @param bucket
* @return
*/
public static List<String> thumbnail2List(String urls, String bucket){
List<String> urllist = new ArrayList<String>();
if(!StringUtil.isEmpty(urls)){
String[] words = urls.split("\\s+|[,,]");
for(String word : words){
if(!StringUtil.isEmpty(words)){
urllist.add(OSSFileUtil.getThumbnail(bucket, word, Global.OSSIMAGE_480P));
}
}
}
return urllist;
}
/**
*
* 上传文件 2015年7月2日14:11:27
*
* @param key 服务器的文件名称
* @param file 要上传的文件
* @param serviceName
* 那个服务器要访问的路径
* @return
* @throws OSSException
* @throws ClientException
* @throws FileNotFoundException
*/
public static String OSSPutObject(String bucketName, String key, File file, String serviceName) {
ObjectMetadata objectMeta = new ObjectMetadata();
objectMeta.setContentLength(file.length());
// 可以在metadata中标记文件类型
// objectMeta.setContentType("image/jpeg");
InputStream input;
try {
input = new FileInputStream(file);
client.putObject(bucketName, serviceName + "/" + key, input, objectMeta);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
return key;
//return "http://" + Global.OSSIMAGE + ".oss-cn-hangzhou.aliyuncs.com/" + serviceName + "/" + key;
}
public static String OSSPutObject(String bucketName, String key, MultipartFile multipartFile , String serviceName) {
CommonsMultipartFile commonsMultipartFile= (CommonsMultipartFile)multipartFile;
DiskFileItem diskFileItem = (DiskFileItem)commonsMultipartFile.getFileItem();
File file = diskFileItem.getStoreLocation();
String fileSuffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
return OSSPutObject(bucketName, key+fileSuffix, file, serviceName);
}
public static void main(String[] args) {
System.out.println(getUrl("mmdimage","1415089949-1.jpg"));
System.out.println(getThumbnail("mmdimage","2015-04-05_19:30:419loW5IZG.jpg", "index2"));
System.out.println(getThumbnail("mmdimage","2015-04-05_19:30:419loW5IZG.jpg", "facestyle"));
System.out.println(getThumbnail("mmdvideo","201504080129071.mov", "index2"));
}
}
后台用的这个里面的方法,几个方法都试了,图片还是模糊。能抽空帮忙看看嘛,拜托
@zhuzhukangkang:
getThumbnail这个方法都写清楚了啊。。生成带签名的缩略图 缩略图 缩略图
话说,不是用getUrl取得地址即可?
@盟怀部孩://远程连接地址,直接返回
if(urlfile.indexOf("http://")>=0 || urlfile.indexOf("https://")>=0){
return urlfile;
}
Date exp = CommonUtil.getMonthLastDate();
URL url = client.generatePresignedUrl(bucket, urlfile, exp, HttpMethod.GET);
//System.out.println(url.toString());
System.out.println(url.toString().replaceFirst("http", "https"));
return url.toString().replaceFirst("http", "https");
}
是应该用这个方法吧?
@zhuzhukangkang: 是的。这个取到的是原图。不过最好用getUrl,这个是带签名的地址,安全
@盟怀部孩:
@zhuzhukangkang: 这个方法改下即可。改动的那句标红色了。
/**
* 将字符串资源转换为List资源
* @param urls
* @return
*/
public static List<String> transform2List(String urls, String bucket){
List<String> urllist = new ArrayList<String>();
if(!StringUtil.isEmpty(urls)){
String[] words = urls.split("\\s+|[,,]");
for(String word : words){
if(!StringUtil.isEmpty(word)){
urllist.add(OSSFileUtil.getUrl(bucket, word));
}
}
}
return urllist;
}
怎么下载.
http://mmdimage.oss-cn-hangzhou.aliyuncs.com/201612201510288091717_1242x2208_0.5625?Expires=1482224773&OSSAccessKeyId=TMP.AQGQzBiSkEI72VeMtqUiCeUvVLOPxrzCSkCCEAEzrbnvHdZYaWRsbeey-wWxADAtAhR0y32NDxEjh2PYuuMHoKIgNqrPpwIVAJjqQyqqCAYwziBCUZrm8sS7L6lj&Signature=1DNRjnBiTkT%2F9vaMZljsbrjbyXk%3D这个是阿里云的图片
https://imgs.manmanda.cn/201612201510288091717_1242x2208_0.5625%40%21index2?Expires=1483199940&OSSAccessKeyId=Q8bYUwi7QQ8qngvN&Signature=VBIFoEIDrCWzsbdQD7hZOvbV8N0%3D这个是后台生成的链接的图片
图片被压缩了?
换个下载图片方法
阿里云的图片存储有一个filter,会进行图片的压缩/水印等设置,当然这个是对外网IP而言,阿里设置这个的目的我想是为了配合他的CDN加速服务。
建议使用内网IP下载到某服务器上,再ftp到本地。
PS:不过我记得阿里图片存储有个什么OSS的客户端工具,可以直接下载,你自己找找。