/**
* 将指定路径的文件转成二进制流返回出来
* @param zipfile 文件路径
* @return
*/
public static byte[] fileToByte(String path){
File zipfile = new File(path);
FileInputStream fis = null;
byte[] b = null;
try {
fis = new FileInputStream(zipfile);
b = new byte[fis.available()];
fis.read(b);
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return b;
}
/*
* 文件导出到深证通目录
* @param bytes 要写入的流文件
* @param type 写入文件的类型
* @param fileName 写入的文件名
* @param szt 深证通目录
*
*/
public static void writeFile(byte[] bytes,String type,String fileName,String szt) throws IOException{
OutputStream ouputStream = null;
String fileNameStr="";
String sztMuLu="";
int size = 0;
if(bytes != null){
size = bytes.length;
}else{
bytes = new byte[0];
}
String now2StrDate = DateTimeUtils.now2StrDate();
String now=now2StrDate.replaceAll("-","");
if(szt==null) {
System.out.println("深证通目录为空");
return;
}
if(szt.indexOf("${date}")>=0) {
sztMuLu=szt.replace("${date}", now);
}else if(szt.indexOf("{date}")>=0) {
sztMuLu=szt.replace("{date}", now);
}else {
sztMuLu=szt;
}
File file=new File(sztMuLu);
file.mkdirs();
if("1".equals(type)){
fileNameStr=fileName+".xml";
}else if("2".equals(type)){
fileNameStr=fileName+".sign";
}else if("3".equals(type)){
fileNameStr=fileName+".zip";
}else if("ok".equals(type)) {
fileNameStr=fileName+".ok";
}
ouputStream = new FileOutputStream(sztMuLu+"\\"+fileNameStr);
ouputStream.write(bytes, 0, size);
ouputStream.flush();
ouputStream.close();
}
/**
* 文件下载
* @param bytes 需要下载的流
* @param type 设置后缀名 1.xml 2.sign 3.zip
* @param fileName 文件名
* @param res HttpServletResponse
* @return true下载成功 false下载失败
* @throws IOException 抛出IO异常必须捕获
*/
public static boolean fileDownLoad(byte[] bytes,String type,String fileName,HttpServletResponse res) throws IOException{
OutputStream ouputStream = null;
int size = 0;
if(bytes != null){
size = bytes.length;
}else{
bytes = new byte[0];
return false;
}
res.reset();
String fileNameStr = URLEncoder.encode(fileName, "UTF-8");
res.setContentType("application/x-msdownload");
res.setCharacterEncoding("UTF-8");
if("1".equals(type)){
res.setHeader("Content-Disposition", "attachment; filename=\""+ fileNameStr + ".xml\"");
}else if("2".equals(type)){
res.setHeader("Content-Disposition", "attachment; filename=\""+ fileNameStr + ".sign\"");
}else if("3".equals(type)){
res.setHeader("Content-Disposition", "attachment; filename=\""+ fileNameStr + ".zip\"");
}else if("4".equals(type)) {
res.setHeader("Content-Disposition", "attachment; filename=\""+ fileNameStr + ".xlsx\"");
}
res.setContentLength(size);
ouputStream = res.getOutputStream();
ouputStream.write(bytes, 0, size);
ouputStream.flush();
ouputStream.close();
return true;
}
/**
* 将多个二进制图片压缩到指定目录
* @param fileList 图片集合
* @param zipfile 压缩目录
*/
public static byte[] ZipFiles(List<byte[]> fileList) {
try {
ByteArrayOutputStream fileOutput = new ByteArrayOutputStream();
ZipOutputStream out = new ZipOutputStream(fileOutput);
out.setMethod(ZipOutputStream.DEFLATED);
for (int i = 0; i < fileList.size(); i++) {
byte[] file = fileList.get(i);
out.putNextEntry(new ZipEntry("Image("+(i+1)+").jpg"));
out.write(file,0,file.length);
out.closeEntry();
}
out.flush();
out.close();
fileOutput.flush();
fileOutput.close();
return fileOutput.toByteArray();
} catch (ZipException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**判断字节流是否为压缩文件
* 如果不为压缩文件将抛出Exception异常
* @param data byte[]
* @return byte[]
*/
public static byte[] unZip(byte[] data) {
byte[] b = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
ZipInputStream zip = new ZipInputStream(bis);
while (zip.getNextEntry() != null) {
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = zip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
}
zip.close();
bis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
}
/**
* 将多个文件转转成压缩包字节流
* @param map 文件集合<文件名,文件字节流>
* @return 压缩包字节流
*/
public static byte[] ZipFiles(Map<String, byte[]> map) {
try {
ByteArrayOutputStream fileOutput = new ByteArrayOutputStream();
ZipOutputStream out = new ZipOutputStream(fileOutput);
out.setMethod(ZipOutputStream.DEFLATED);
Set<String> key = map.keySet();
for (Iterator it = key.iterator(); it.hasNext();) {
String fileName = (String) it.next();
byte[] file = map.get(fileName);
out.putNextEntry(new ZipEntry(fileName));
out.write(file,0,file.length); //压缩流zipOut写入底层流byteArrayOut
out.closeEntry();
}
out.flush();
out.close();
fileOutput.flush();
fileOutput.close();
return fileOutput.toByteArray();
} catch (ZipException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**将目标流进行解压缩
* @param bytes
* @return
*/
public static Map<String,byte[]> deCompress(byte[] bytes) {
int n;
byte[] buffer = new byte[256];
Map<String,byte[]> map=new HashMap<String,byte[]>();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ZipInputStream unzip = new ZipInputStream(in);
try {
ZipEntry entry=unzip.getNextEntry();
while(entry!=null) {
while ((n = unzip.read(buffer)) >= 0) {
out.write(buffer,0,n);
}
String fileName=entry.getName();
byte[] fileByte=out.toByteArray();
map.put(fileName, fileByte);
out.reset(); //重要:多个文件循环写入时,每读完一个文件要out要reset一下
out.flush();
unzip.closeEntry();
entry=unzip.getNextEntry();
}
}catch(ZipException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}finally{
try {
if(in!=null) {
in.close();
}
if(unzip!=null) {
unzip.close();
}
if(out!=null) {
out.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
return map;
}
/**
* 删除指定路径的单个文件
* @param path 文件目录
*/
public static void deleteFile(String path){
File file = new File(path);
if(file.exists()){
if(file.isFile()){
file.delete();
}
}
}
/*得到上传文件的字节流
* @param req
* */
public static byte[] getUploadByte(HttpServletRequest req) {
byte[] bytes=null;
Map map = new HashMap();
map = ImportUtils.uploadFieldProperties(req);
Set c = map.keySet();
Iterator it = c.iterator();
while(it.hasNext()) {
Object key = it.next();//key
Object value = map.get(key);//value
if(key.equals("signfile")) { //文件域的name属性
bytes=(byte[])value;
}
}
return bytes;
}
/*
* 根据指定的路径得到文件的流
*/
public static byte[] getPathBytes(String path) {
FileInputStream in = null;
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
byte buffer[] = new byte[128];
byte[] bytes=null;
int b;
try {
in = new FileInputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
try {
while (( b=in.read(buffer, 0, buffer.length))!= -1) {
byteOutput.write(buffer, 0, b);
bytes = byteOutput.toByteArray();
//切除字节的尾部的空字节,只读取读到的字节数就行了
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
in.close();
byteOutput.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return bytes;
}