您的位置:首页 > 编程语言 > Go语言

美团云服务器django web平台搭建全过程

2015-11-01 16:56 555 查看
第一种形式,是以流的开式直接response,适用于数据量不是很大的情况下。

第二种形式,是先将要下载的文件写到后台的一个文件中,然后再进行下载。

package com.lh446.action;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

public class ExportAction extends MappingDispatchAction {
/**
* 直接通过流导出
* @param arg0
* @param arg1
* @param arg2
* @param arg3
* @return
* @throws Exception
*/
public ActionForward export(ActionMapping arg0, ActionForm arg1, HttpServletRequest req, HttpServletResponse res) throws Exception {

String msg="倒萨大额外人玩儿完";
String fileName="test1.txt";
//fileName=new String(fileName.getBytes("iso-8859-1"),"utf-8");
res.setContentType("text/plain;charset=GBK");
res.addHeader("Content-disposition", "attachment;filename="+fileName);
BufferedOutputStream out=new BufferedOutputStream(res.getOutputStream());
out.write(msg.getBytes("GBK"));
out.close();
return null;
}
/**
* 先将数据保存在本地(本实例通过TXT文本演示)在下载
* @param arg0
* @param arg1
* @param arg2
* @param arg3
* @return
* @throws Exception
*/
public ActionForward export2(ActionMapping arg0, ActionForm arg1, HttpServletRequest req, HttpServletResponse res) throws Exception {
String msg="倒萨大额外人玩儿完";
String path = new String((this.getServlet().getServletContext()
.getRealPath("/") + "/export/").getBytes(), "GBK");// 存放路径
File file=new File(path+"导出测试二.txt");
//创建新文件
if(file.exists()){
file.delete();
}
file.getParentFile().mkdirs();
file.createNewFile();
FileOutputStream fout=new FileOutputStream(file);
BufferedOutputStream buf=new BufferedOutputStream(fout);
buf.write(msg.getBytes());
buf.close();//写入都本地文件中
res.setContentType("text/plain;charset=GBK");
res.addHeader("Content-disposition", "attachment;filename=test2.txt");
BufferedOutputStream out=new BufferedOutputStream(res.getOutputStream());
FileInputStream in=new FileInputStream(path+"导出测试二.txt");
byte[] b=new byte[1024];
int i=0;
while((i=in.read(b))>0){
out.write(b, 0, i);
out.flush();
}
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
return null;
}
/**
* 先将数据保存在本地并压缩处理(保存为zip文件)在下载
* @param arg0
* @param arg1
* @param arg2
* @param arg3
* @return
* @throws Exception
*/
public ActionForward export3(ActionMapping arg0, ActionForm arg1, HttpServletRequest req, HttpServletResponse res) throws Exception {
String msg="倒萨大额外人玩儿完";
String path = new String((this.getServlet().getServletContext()
.getRealPath("/") + "/export/").getBytes(), "GBK");// 存放路径
List<String> fileNames=new ArrayList<String>();
fileNames.add("test3.txt");
File file=new File(path+"test3.txt");
//  创建新文件
if(file.exists()){
file.delete();
}
file.getParentFile().mkdirs();
file.createNewFile();
FileOutputStream fout=new FileOutputStream(file);
BufferedOutputStream buf=new BufferedOutputStream(fout);
buf.write(msg.getBytes());
buf.close();//写入都本地文件中
String zipName="test3.zip";
tozip(fileNames,path,zipName);
res.setContentType("application/zip zip;charset=UTF-8");
res.addHeader("Content-Disposition", "attachment;filename="
+ zipName);
OutputStream outp = res.getOutputStream();
FileInputStream in=new FileInputStream(path+zipName);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) > 0) {
outp.write(b, 0, i);
outp.flush();
}
if (in != null) {
in.close();
in = null;
}
if (outp != null) {
outp.close();
outp = null;
}
return null;
}
//压缩处理:
public void tozip(List<String> fileNames,String path,String zipName) throws IOException{
//  新建zip文件
ZipOutputStream zipout=null;
FileOutputStream fout=new FileOutputStream(path+zipName);
zipout=new ZipOutputStream(new BufferedOutputStream(fout));
for(int i=0;i<fileNames.size();i++){
ZipEntry zipEntry=new ZipEntry(fileNames.get(i));//设置zip文件中每个文件的名称
zipout.putNextEntry(zipEntry);
FileInputStream in=new FileInputStream(path+fileNames.get(i));//通过文件流取得当前要压缩文件的数据
byte[] b=new byte[1024];
int a=0;
while((a=in.read(b))>0){
zipout.write(b, 0, a);//写入到当前设置的zipEntry中,即zip文件中的一个压缩文件
zipout.flush();
}
in.close();
}
zipout.close();
}
}


页面请求代码:(通过form表单请求)

function exportData(){
var form=document.dataForm;
var url="/test/export.do";
form.action=url;
form.submit();
}






假如页面是二级页面(即通过window.showModalDialog(src,window,style)打开)则此时无法打开下载框。

解决方法:1。在form中设置target="_self",此时会新打开一个页面下载(不友好)

2。。通过iframe框架解决:

<iframe id="hidden_iframe" name="hidden_iframe" style="display: none"></iframe>
<form id="dataform" name="dataform" method="post" target="hidden_iframe"></form>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: