导出txt
@RequestMapping(value = "/txt")
public void exportTxt(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/plain");
String fileName = "产品信息";
BufferedOutputStream buff = null;
StringBuffer write = new StringBuffer();
ServletOutputStream outSTr = null;
try {
response.setHeader("Content-Disposition",
"attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8") + ".txt");
outSTr = response.getOutputStream();
buff = new BufferedOutputStream(outSTr);
String tab = "\r\n";
String tcb = "\t";
write.append("产品名称" + tcb);
write.append("品牌名称" + tcb);
write.append("商品条码" + tcb);
write.append("标准号" + tcb);
write.append("标准名称" + tcb);
write.append("标准分类" + tcb);
write.append("产品规格" + tcb);
write.append("保质期" + tcb);
write.append("产品成分" + tcb);
write.append("下单总数量" + tcb + tab);
buff.write(write.toString().getBytes("UTF-8"));
buff.flush();
buff.close();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
buff.close();
outSTr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
导出txt 标准操作
@RequestMapping(value = "/dldt")
public void downloadtxt(HttpServletRequest request, HttpServletResponse response) {
StringBuffer text = new StringBuffer();
text.append("log.getOpeuser()");
text.append("|");
text.append("log.getOpedesc()");
text.append("|");
text.append("dateString" + new Date().toString());
text.append("\r\n");
exportTxt(response,text.toString());
}
public static void exportTxt(HttpServletResponse response, String text) {
response.setCharacterEncoding("utf-8");
response.setContentType("text/plain");
response.addHeader("Content-Disposition",
"attachment;filename=" + genAttachmentFileName("文件名称", "JSON_FOR_UCC_")
+ ".txt");
BufferedOutputStream buff = null;
ServletOutputStream outStr = null;
try {
outStr = response.getOutputStream();
buff = new BufferedOutputStream(outStr);
buff.write(text.getBytes("UTF-8"));
buff.flush();
buff.close();
} catch (Exception e) {
} finally {
try {
buff.close();
outStr.close();
} catch (Exception e) {
}
}
}
public static String genAttachmentFileName(String cnName, String defaultName) {
try {
cnName = new String(cnName.getBytes("gb2312"), "ISO8859-1");
} catch (Exception e) {
cnName = defaultName;
}
return cnName;
}
zip下载
public static void filesToZip(List<File> srcFiles, String targetFileName) throws IOException {
String fileOutName = targetFileName + ".zip";
byte[] buf = new byte[1024*1024];
FileInputStream in = null;
FileOutputStream fos = null;
ZipOutputStream out = null;
try {
fos = new FileOutputStream(fileOutName);
out = new ZipOutputStream(fos);
for (File file : srcFiles) {
in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.getName()));
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
if (in != null) {
in.close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (fos != null) {
out.closeEntry();
out.close();
fos.close();
}
}
}
导出ZIP
package com.example.demo;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipThread {
static final int BUFFER = 8192;
private File zipFile;
public ZipThread(String pathName) {
zipFile = new File(pathName);
}
public void compress(String... pathName) {
ZipOutputStream out = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
out = new ZipOutputStream(cos);
String basedir = "";
for (int i = 0; i < pathName.length; i++) {
compress(new File(pathName[i]), out, basedir);
}
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void compress(String srcPathName) {
File file = new File(srcPathName);
if (!file.exists())
throw new RuntimeException(srcPathName + "不存在!");
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void compress(File file, ZipOutputStream out, String basedir) {
if (file.isDirectory()) {
System.out.println("压缩:" + basedir + file.getName());
this.compressDirectory(file, out, basedir);
} else {
System.out.println("压缩:" + basedir + file.getName());
this.compressFile(file, out, basedir);
}
}
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
compress(files[i], out, basedir + dir.getName() + "/");
}
}
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
ZipThread zc = new ZipThread("C:\\Users\\63575\\Desktop\\07/ce.zip");
zc.compress("C:\\data\\lib","C:\\home\\yanxiuhair\\logs","C:\\mvn\\src\\test\\java");
}
}
文件下载工具类
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class DownloadUtil {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamUtils.copy(baos.toByteArray(), os);
protected void download(String filePath,String returnName,HttpServletResponse response,boolean delFlag){
this.prototypeDownload(new File(filePath), returnName, response, delFlag);
}
protected void download(File file,String returnName,HttpServletResponse response,boolean delFlag){
this.prototypeDownload(file, returnName, response, delFlag);
}
public void prototypeDownload(File file,String returnName,HttpServletResponse response,boolean delFlag){
FileInputStream inputStream = null;
ServletOutputStream outputStream = null;
try {
if(!file.exists()) return;
response.reset();
response.setContentType("application/octet-stream;charset=utf-8");
returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1"));
response.addHeader("Content-Disposition", "attachment;filename="+returnName);
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
int length = 1024;
int readLength=0;
byte buf[] = new byte[1024];
readLength = inputStream.read(buf, 0, length);
while (readLength != -1) {
outputStream.write(buf, 0, readLength);
readLength = inputStream.read(buf, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
if(delFlag) {
file.delete();
}
}
}
public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException{
response.setContentType("application/octet-stream;charset=utf-8");
returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1"));
response.addHeader("Content-Disposition", "attachment;filename=" + returnName);
response.setContentLength(byteArrayOutputStream.size());
ServletOutputStream outputstream = response.getOutputStream();
byteArrayOutputStream.writeTo(outputstream);
byteArrayOutputStream.close();
outputstream.flush();
}
}