导出另存为如何实现? 用jsp实现:点击一个连接后 导出一段文本并弹出另存为窗口? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 response.setContentType("application/*"); response.setHeader("Content-Disposition", "attachment; filename=\"" + attachment.getFileName() + "\""); OutputStream os = response.getOutputStream(); byte[] data = new byte[4096]; int result = 0; while (true) { result = fis.read(data); if (result != -1) { os.write(data); } else { break; } } fis.close(); os.flush();一个例子 参考一下吧, 里面的fis 是一个fileinputstream ,attachment 是一个自己定义的类 getFileName返回的是下载文件的名字,你要做的就是修改往os便俩国内里面写入数据的部分代码 看看这个文章,你会有所启发的。http://www.java2000.net/viewthread.jsp?tid=179 请看我的 下载程序 ,这个 比 网上的应该好 ,我是结合了网提供的 资源经过总结后 写出来的 ~oo~,其中 fileFullName 变量就是 你要下载 文件的 路径 , fileHaderName变量是 你 要显示给客户的 文件 名字,里面已经 解决了 下载 可能出现 中文乱麻问题,而且不需要 用 byte[] 数组, 直接 用流读取, 多大的文件多不会出问题package com.aspire.report.web.servlet;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.aspire.report.common.constant.DownLoadFileConstant;import com.aspire.report.common.constant.ReportConstant;import com.aspire.report.common.resource.Const;import com.aspire.report.para.Assembled;import com.aspire.simf.eaf.client.Staff;public class AccountDownLoadServlet extends HttpServlet { private static final long serialVersionUID = 1L; public AccountDownLoadServlet() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { FileInputStream in = null; // 输入流 ServletOutputStream out = null; // 输出流 String mainDeptID = new String(request.getParameter("deptID").getBytes("GBK")); String reportID = new String(request.getParameter("reportID").getBytes("GBK")); request.setCharacterEncoding("GBK"); String para = new String(request.getParameter("para").getBytes("GBK")); String type = new String(request.getParameter("type").getBytes("GBK")); String name = mainDeptID+ "_" + para + DownLoadFileConstant.FILE_TYPE ; String filePath = ""; String needDownloadFolder = null; if( ReportConstant.PFR_STAT33.equals(reportID)) { needDownloadFolder = DownLoadFileConstant.KPI_DOWNLOAD_FILE_FOLDER_NAME; } else { needDownloadFolder = DownLoadFileConstant.DETAIL_DOWNLOAD_FILE_FOLDER_NAME; } if(type.equals("m")) { filePath = Assembled.getAccountDownLoadFilePath() + File.separator + needDownloadFolder + File.separator + DownLoadFileConstant.MONTH_DOWNLOAD_FILE_FOLDER_NAME + File.separator + mainDeptID + File.separator+ para+ File.separator; } else { filePath = Assembled.getAccountDownLoadFilePath() + File.separator + needDownloadFolder + File.separator + DownLoadFileConstant.DATE_DOWNLOAD_FILE_FOLDER_NAME + File.separator + mainDeptID + File.separator+ para+ File.separator; } response.setContentType("application/x-msdownload"); String fileHaderName = URLEncoder.encode(name, "UTF-8"); response.setHeader("Content-disposition", "attachment; filename=" + fileHaderName); String fileFullName = filePath + name ; try { in = new FileInputStream(fileFullName); // 读入文件 out = response.getOutputStream(); out.flush(); int aRead = 0; while ((aRead = in.read()) != -1 & in != null) { out.write(aRead); } out.flush(); } catch (Throwable e) { // log.error("FileDownload doGet() IO error!",e); } finally { try { in.close(); out.close(); } catch (Throwable e) { // log.error("FileDownload doGet() IO close error!",e); } } }catch(Exception e) { e.printStackTrace(); } }} hibernate3.jar has no source attachment 在网页中嵌入realPlayer播放器,播放不了.wmv格式的文件,提示版本低,其他都能播放,请问我该如何修改? 请手请教:关于考勤系统的问题 高分求开发BBS的用户需求说明书,偶是学生 熟悉java mail的高手请进来看下,很简单! strust问题:在JSP页面中如何用java代码<% %>实现<bean:write>的功能 如何把response 中的内容统一封装成json格式后返回 急!! 菜鸟问题 奋斗中的少女求技术小伙伴 求助!如何加密图片文件 求查询结果传入下一个查询语句
response.setHeader("Content-Disposition", "attachment; filename=\""
+ attachment.getFileName() + "\"");
OutputStream os = response.getOutputStream();
byte[] data = new byte[4096];
int result = 0;
while (true) {
result = fis.read(data);
if (result != -1) {
os.write(data);
} else {
break;
}
}
fis.close();
os.flush();
一个例子 参考一下吧, 里面的fis 是一个fileinputstream ,attachment 是一个自己定义的类 getFileName返回的是下载文件的名字,你要做的就是修改往os便俩国内里面写入数据的部分代码
,其中 fileFullName 变量就是 你要下载 文件的 路径 , fileHaderName变量是 你 要显示给客户的 文件 名字,里面已经 解决了 下载 可能出现 中文乱麻问题,而且不需要 用 byte[] 数组, 直接 用流读取, 多大的文件多不会出问题
package com.aspire.report.web.servlet;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.aspire.report.common.constant.DownLoadFileConstant;
import com.aspire.report.common.constant.ReportConstant;
import com.aspire.report.common.resource.Const;
import com.aspire.report.para.Assembled;
import com.aspire.simf.eaf.client.Staff;
public class AccountDownLoadServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public AccountDownLoadServlet() {
super();
} public void destroy() {
super.destroy();
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
FileInputStream in = null; // 输入流
ServletOutputStream out = null; // 输出流
String mainDeptID = new String(request.getParameter("deptID").getBytes("GBK"));
String reportID = new String(request.getParameter("reportID").getBytes("GBK"));
request.setCharacterEncoding("GBK");
String para = new String(request.getParameter("para").getBytes("GBK"));
String type = new String(request.getParameter("type").getBytes("GBK"));
String name = mainDeptID+ "_" + para + DownLoadFileConstant.FILE_TYPE ;
String filePath = "";
String needDownloadFolder = null;
if( ReportConstant.PFR_STAT33.equals(reportID)) {
needDownloadFolder = DownLoadFileConstant.KPI_DOWNLOAD_FILE_FOLDER_NAME;
} else {
needDownloadFolder = DownLoadFileConstant.DETAIL_DOWNLOAD_FILE_FOLDER_NAME;
}
if(type.equals("m")) {
filePath = Assembled.getAccountDownLoadFilePath()
+ File.separator + needDownloadFolder + File.separator
+ DownLoadFileConstant.MONTH_DOWNLOAD_FILE_FOLDER_NAME
+ File.separator + mainDeptID + File.separator+ para+ File.separator;
} else {
filePath = Assembled.getAccountDownLoadFilePath()
+ File.separator + needDownloadFolder + File.separator
+ DownLoadFileConstant.DATE_DOWNLOAD_FILE_FOLDER_NAME
+ File.separator + mainDeptID + File.separator+ para+ File.separator;
}
response.setContentType("application/x-msdownload");
String fileHaderName = URLEncoder.encode(name, "UTF-8");
response.setHeader("Content-disposition", "attachment; filename="
+ fileHaderName);
String fileFullName = filePath + name ;
try {
in = new FileInputStream(fileFullName); // 读入文件
out = response.getOutputStream();
out.flush();
int aRead = 0;
while ((aRead = in.read()) != -1 & in != null) {
out.write(aRead);
}
out.flush();
} catch (Throwable e) {
// log.error("FileDownload doGet() IO error!",e);
} finally {
try {
in.close();
out.close();
} catch (Throwable e) {
// log.error("FileDownload doGet() IO close error!",e);
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
}