各种图片操作
取压缩包图片
public static boolean unzipFile(String directory, String zip,String fname,int size) {
boolean falg=false;
try {
log.info("文件名字:"+fname);
ZipInputStream zis = new ZipInputStream(new FileInputStream(new File(zip)));
ZipEntry ze = zis.getNextEntry();
log.info("ze--->"+ze);
File parent = new File(directory);
int i=1;
while(ze == null){
ze=zis.getNextEntry();
i++;
if(i>=size){
break;
}
}
while (ze != null){
String name = ze.getName();
log.info("name---》"+name);
if(name.equals(fname)){
File child = new File(parent, name);
FileOutputStream output = new FileOutputStream(child);
byte[] buffer = new byte[10240];
int bytesRead = 0;
while ((bytesRead = zis.read(buffer)) > 0)
{
output.write(buffer, 0, bytesRead);
falg=true;
}
output.flush();
output.close();
ze = zis.getNextEntry();
zis.close();
break;
} else {
ze=zis.getNextEntry();
}
}
}
catch (IOException e) {
log.info(e.getMessage());
}
return falg;
}
生成图片方寸压缩包
public static boolean add(String zipUrl,String fromUrl,String toUrl) {
Map <String,String> env = new HashMap <>();
env.put("create","true");
Path path = Paths.get(zipUrl);
URI uri = URI.create("jar:"+path.toUri());
try(FileSystem zipfs = FileSystems.newFileSystem(uri,env)){
Path externalTxtFile = Paths.get(fromUrl);
Path pathInZipfile = zipfs.getPath(toUrl);
Files.copy(externalTxtFile,pathInZipfile,StandardCopyOption.REPLACE_EXISTING);
return true;
}catch (Exception e) {
return false;
}
}
工具类
package com.kanq.jining.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.itextpdf.text.pdf.PdfStructTreeController.returnType;
public class QTUtils {
public static Logger log = LoggerFactory.getLogger(QTUtils.class);
public static String getValue(String name) {
Properties properties = new Properties();
String value = "";
try
{
properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
value = new String(properties.getProperty(name).getBytes("iso-8859-1"), "utf-8");
}
catch (IOException e)
{
e.printStackTrace();
}
return value;
}
public static boolean unzipFile(String directory, String zip,String fname,int size) {
boolean falg=false;
try {
log.info("文件名字:"+fname);
ZipInputStream zis = new ZipInputStream(new FileInputStream(new File(zip)));
ZipEntry ze = zis.getNextEntry();
log.info("ze--->"+ze);
File parent = new File(directory);
int i=1;
while(ze == null){
ze=zis.getNextEntry();
i++;
if(i>=size){
break;
}
}
while (ze != null){
String name = ze.getName();
log.info("name---》"+name);
if(name.equals(fname)){
File child = new File(parent, name);
FileOutputStream output = new FileOutputStream(child);
byte[] buffer = new byte[10240];
int bytesRead = 0;
while ((bytesRead = zis.read(buffer)) > 0)
{
output.write(buffer, 0, bytesRead);
falg=true;
}
output.flush();
output.close();
ze = zis.getNextEntry();
zis.close();
break;
} else {
ze=zis.getNextEntry();
}
}
}
catch (IOException e) {
log.info(e.getMessage());
}
return falg;
}
public static String creatml(String slid, String ml)
{
Calendar cal = Calendar.getInstance();
int d = cal.get(5);
int m = cal.get(2) + 1;
String month = "";
String day = "";
if (m < 10) {
month = "0" + m;
} else {
month = "" + m;
}
if (d < 10) {
day = "0" + d;
} else {
day = "" + d;
}
int year = cal.get(1);
String dizhi = ml + "/" + year + "/" + month + "/" + day + "/" + slid + "/";
File dir = new File(dizhi);
if (!dir.exists())
{
dir.mkdirs();
System.out.println("创建目录成功");
}
String DZ = year + "/" + month + "/" + day + "/"+ slid + "/";
return DZ;
}
public static String creatWJ(String slid, String ml){
String tzwjdz = QTUtils.getValue("tzwjdz");
String mldz = tzwjdz + "/" + slid ;
String dizhi = tzwjdz + "/" + slid +"/"+slid+".zip";
File dir = new File(mldz);
if (!dir.exists()){
try {
dir.mkdirs();
log.info("创建文件夹成功");
} catch (Exception e) {
e.printStackTrace();
}
}
String generateFile=null;
try {
generateFile = generateFile( mldz, "zip");
} catch (Exception e) {
e.printStackTrace();
}
return generateFile;
}
public static net.sf.json.JSONObject jsonobject(Map<String, Object> datalist) {
String str = com.alibaba.fastjson.JSONArray.toJSONString(datalist, filter, SerializerFeature.WriteNullBooleanAsFalse);
final net.sf.json.JSONObject fromObject = net.sf.json.JSONObject.fromObject(str);
return fromObject;
}
private static ValueFilter filter = new ValueFilter() {
public Object process(Object obj, String s, Object v) {
if (v == null) {
return "";
}
return v;
}
};
public static String jsonString(Map<String, Object> datalist) {
String str = com.alibaba.fastjson.JSONArray.toJSONString(datalist, filter, SerializerFeature.WriteNullBooleanAsFalse);
str = str.replaceAll("\":\\s*([^\\[{\"])(.*?)(?=[[,\"]|[},]}])", "\":\"$1$2\"");
return str;
}
public static void main (String[] args){
String creatml = creatWJ("124", "d://work/ftp");
System.err.println(creatml);
}
public static Object getDIC(Object key) {
final Properties pros = new Properties();
String value = "";
try {
pros.load(new InputStreamReader(PropertiesUtil.class.getResourceAsStream("/dic.properties"), "UTF-8"));
value = pros.getProperty(key.toString());
} catch (Exception e) {
return e.getMessage();
}
return value;
}
public static boolean add(String zipUrl,String fromUrl,String toUrl) {
Map <String,String> env = new HashMap <>();
env.put("create","true");
Path path = Paths.get(zipUrl);
URI uri = URI.create("jar:"+path.toUri());
try(FileSystem zipfs = FileSystems.newFileSystem(uri,env)){
Path externalTxtFile = Paths.get(fromUrl);
Path pathInZipfile = zipfs.getPath(toUrl);
Files.copy(externalTxtFile,pathInZipfile,StandardCopyOption.REPLACE_EXISTING);
return true;
}catch (Exception e) {
return false;
}
}
public static boolean deleteFile(String pathname){
boolean result = false;
File file = new File(pathname);
if (file.exists())
{
file.delete();
result = true;
log.info("文件已经被成功删除");
}
return result;
}
public static String generateFile(String path, String format) throws Exception {
File file = new File(path);
if (!file.exists()) {
throw new Exception("路径 " + path + " 不存在文件,无法进行压缩...");
}
String generateFile = file.getParent() + File.separator ;
File compress = new File(generateFile);
if( !compress.exists() ){
compress.mkdirs();
}
String generateFileName = compress.getAbsolutePath() + File.separator + file.getName() + "." + format;
FileOutputStream outputStream = new FileOutputStream(generateFileName);
ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(outputStream));
generateFile(zipOutputStream,file,"");
log.info("源文件位置:" + file.getAbsolutePath() + ",目的压缩文件生成位置:" + generateFileName);
zipOutputStream.close();
return generateFileName;
}
private static void generateFile(ZipOutputStream out, File file, String dir) throws Exception {
if (file.isDirectory()) {
File[] files = file.listFiles();
out.putNextEntry(new ZipEntry(dir + "/"));
dir = dir.length() == 0 ? "" : dir + "/";
for (int i = 0; i < files.length; i++) {
generateFile(out, files[i], dir + files[i].getName());
}
} else {
FileInputStream inputStream = new FileInputStream(file);
out.putNextEntry(new ZipEntry(dir));
int len = 0;
byte[] bytes = new byte[1024];
while ((len = inputStream.read(bytes)) > 0) {
out.write(bytes, 0, len);
}
inputStream.close();
}
}
}