import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import com.drew.imaging.FileType;
import com.drew.imaging.FileTypeDetector;
/**
* 功能:ZIMG图片上传
*
* @author 任梁荣
* */
public class ZimgUtils {
protected static Logger logger = LoggerFactory.getLogger(ZimgUtils.class);
public static void main(String[] args) {
System.out.println(ZimgUtils.uploadImage("http://images.renlr.com", "/Users/liang/Downloads/timg.jpg"));
}
public static void mkdirs(String path) {
if (path != null) {
File f = new File(path);
if (!f.exists()) {
f.mkdirs();
}
}
}
public static String sha1ToPath(String sha1) {
String result = String.valueOf("");
if (sha1 != null && sha1.trim().length() > 0) {
String s1 = sha1.substring(0, 1);
String s2 = DigestUtils.sha1Hex(sha1.getBytes()).substring(0, 2);
result = s1 + File.separator + s2 + File.separator + sha1;
}
return result;
}
public static String readExt(String image) {
try {
if (image != null && image.trim().length() > 0) {
File file = new File(image);
if (file.exists()) {
FileInputStream input = new FileInputStream(file);
BufferedInputStream binput = new BufferedInputStream(input);
FileType fileType = FileTypeDetector.detectFileType(binput);
return fileType.toString();
}
}
} catch (Exception e) {
logger.error("读取图片ext信息失败", e);
}
return null;
}
/**
* 功能:从页面提交图片上传到图片资源中心
* */
public static List submitImage(HttpServletRequest request, String fileTag, String url) {
List result = new ArrayList();
MultipartHttpServletRequest mhs = (MultipartHttpServletRequest) request;
try {
List files = mhs.getFiles(fileTag);
if (files != null && files.size() > 0) {
for (MultipartFile $file : files) {
if (!$file.isEmpty()) {
String $name = $file.getOriginalFilename();
String ext = $name.substring($name.lastIndexOf("."), $name.length());
String sha1 = DigestUtils.sha1Hex($file.getInputStream());
String fileName = sha1 + ext;
String path = System.getProperty("java.io.tmpdir") + File.separator + sha1ToPath(sha1);
mkdirs(path);
FileUtils.copyInputStreamToFile($file.getInputStream(), new File(path, fileName));
String imageUrl = uploadImage(url, path + File.separator + fileName);
if (imageUrl != null && imageUrl.trim().length() > 0) {
result.add(imageUrl);
File f = new File(path, fileName);
f.setWritable(true);
System.gc();
f.delete();
}
}
}
}
} catch (Exception e) {
logger.error("上传图片失败:", e);
}
return result;
}
/**
* 功能:将图片上传图片资源中心
* */
public static String uploadImage(String url, String file) {
try {
String ext = readExt(file);
URL $url = new URL(url);
URLConnection connection = $url.openConnection();
connection.setReadTimeout(50000);
connection.setConnectTimeout(25000);
HttpURLConnection uc = (HttpURLConnection) connection;
uc.setRequestMethod("POST");
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setRequestProperty("Cache-Control", "no-cache");
uc.setRequestProperty("Content-Type", ext.toLowerCase());
uc.setRequestProperty("COOKIE", "william");
uc.setDoOutput(true);
uc.setDoInput(true);
uc.connect();
OutputStream output = uc.getOutputStream();
FileInputStream input = new FileInputStream(file);
byte[] buf = new byte[8192];
while (true) {
int len = input.read(buf);
if (len <= 0)
break;
output.write(buf, 0, len);
}
StringBuffer resp = new StringBuffer();
InputStreamReader inReader = new InputStreamReader(uc.getInputStream(), "UTF-8");
char[] bb = new char[8192];
while (true) {
int length = inReader.read(bb);
if (length == -1)
break;
char[] bc = new char[length];
for (int i = 0; i < length; i++)
bc[i] = bb[i];
resp.append(new String(bc));
}
output.close();
input.close();
inReader.close();
uc.disconnect();
JSONObject json = JSONObject.parseObject(resp.toString());
if (json.getBooleanValue("ret")) {
JSONObject info = json.getJSONObject("info");
String md5 = info.getString("md5");
return String.format("%s/%s", url, md5);
}
} catch (Exception e) {
logger.error("上传图片失败", e);
}
return null;
}
/**
* 功能:从网络下载图片上传图片资源中心
* */
public static String downloadUploadImage(String url, String imageUrl) {
try {
String file = downloadImage(imageUrl);
if (file != null && file.trim().length() > 0) {
return uploadImage(url, file);
}
} catch (Exception e) {
logger.error("从网络下载图片上传图片资源中心失败", e);
}
return null;
}
/**
* 功能:从网络下载图片缓存到本地
* */
public static String downloadImage(String imageUrl) {
try {
URL $url = new URL(imageUrl);
DataInputStream input = new DataInputStream($url.openStream());
String $filename = UUID.randomUUID().toString();
String path = System.getProperty("java.io.tmpdir");
String file = String.format("%s%s%s", path, File.separator, $filename);
FileOutputStream output = new FileOutputStream(new File(file));
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
input.close();
output.close();
return file;
} catch (Exception e) {
logger.error("从网络下载图片失败", e);
}
return null;
}
}