本文主要用来从url获取文件输入流,并且将其加入zip的输出流并导出
环境
jfinal
tomcat
jdk1.7
jfinal并不是官网标准的框架。
目录
文件1:GetFile.java
结构:
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class GetFile {
//1.1从url获取文件的输入流
public InputStream getFileInputStream(String url) throws Exception{}
//1.2从url获取文件名
public String getFileName(String url) throws IOException{}
}
public InputStream getFileInputStream(String url) throws Exception{
InputStream murl = new URL(url).openStream();
return murl;
}
public String getFileName(String url) throws IOException {
String filename = "";
boolean isok = false;
// 从UrlConnection中获取文件名称
try {
URL myURL = new URL(url);
URLConnection conn = myURL.openConnection();
if (conn == null) {
return null;
}
Map> hf = conn.getHeaderFields();
if (hf == null) {
return null;
}
Set key = hf.keySet();
if (key == null) {
return null;
}
for (String skey : key) {
List values = hf.get(skey);
for (String value : values) {
String result;
try {
result = new String(value.getBytes("ISO-8859-1"), "GBK");
int location = result.indexOf("filename");
if (location >= 0) {
result = result.substring(location
+ "filename".length());
filename = result
.substring(result.indexOf("=") + 1);
isok = true;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}// ISO-8859-1 UTF-8 gb2312
}
if (isok) {
break;
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return filename;
}
文件2:ZipController.java
结构:
import com.jfinal.core.Controller;
import org.apache.commons.codec.binary.Base64;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
public class ZipController extends Controller {
//2.1
public void index() throws IOException {}
//2.2
public void zipAction(String url,InputStream is,ZipOutputStream zos,GetFile gf,byte[] buffer) throws Exception{}
//2.3
public String encodeFileName(HttpServletRequest request, String fileName) throws UnsupportedEncodingException{}
}
public void index() throws IOException {
String fileName = "List";
HttpServletResponse response = getResponse();
HttpServletRequest request = getRequest();
response.setHeader("Connection", "close");
response.setHeader("Content-Type", "application/vnd.ms-excel;charset=UTF-8");
String filename = System.currentTimeMillis() + fileName+".zip";
filename = encodeFileName(request, filename);
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
OutputStream out = null;
out = response.getOutputStream();
try {
// create byte buffer
byte[] buffer = new byte[1024];
ZipOutputStream zos = new ZipOutputStream(out);
GetFile gf = new GetFile();
InputStream is= null;
String url1 = "http://xxxxxxxxxxx";
String url2 = "http://xxxxxxxxxxx";
zipAction(url1,is,zos,gf,buffer);
zipAction(url2,is,zos,gf,buffer);
zos.closeEntry();
zos.close();
is.close();
}catch (Exception ioe) {
System.out.println("Error creating zip file" + ioe);
}
}
public void zipAction(String url,InputStream is,ZipOutputStream zos,GetFile gf,byte[] buffer) throws Exception{
is = gf.getFileInputStream(url);
String fileName1 = gf.getFileName(url);
zos.putNextEntry(new ZipEntry(fileName1));
int length;
while ((length = is.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
}
public String encodeFileName(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 != agent.indexOf("MSIE")) {
return URLEncoder.encode(fileName, "UTF-8");
} else if (null != agent && -1 != agent.indexOf("Mozilla")) {
return "=?UTF-8?B?"+ (new String(Base64.encodeBase64(fileName.getBytes("UTF-8")))) + "?=";
} else {
return fileName;
}
}