当前位置: 代码迷 >> Java Web开发 >> 导出文件时怎么让用户选择路径
  详细解决方案

导出文件时怎么让用户选择路径

热度:27   发布时间:2016-04-17 16:42:06.0
导出文件时如何让用户选择路径?
我在做将数据库的内容导出为excel文件时,如何使用文件对话框让用户选择生成文件的路径呢?用 <input   type=file   /> 只能选择已有的文件啊。请各位帮忙,谢谢!

------解决方案--------------------
只能用下载的形式
------解决方案--------------------
这应该是一个download的程序阿,说是导出,无非就是让用户下载到本地阿。对吗?


一下使我写的,希望对你有帮助

package login.source.action;
import java.sql.Date;
import java.text.DateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import login.source.fileopration.DbtoFile;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public final class DownloadfileAction extends Action {

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
String path=servlet.getServletContext().getRealPath( "/DownLoadFiles ");
String pathTem = servlet.getServletContext().getRealPath( "/Template ");
String finame = " ";
Date dt = new Date(System.currentTimeMillis());
DateFormat df = DateFormat.getInstance();
finame = df.format(dt);
finame = finame.replace( '/ ', '_ ');
finame =finame.replace( ' ', '_ ');
finame = finame.replace( ': ', '_ ');
finame = finame + String.valueOf(System.currentTimeMillis());


Integer loginId = (Integer)request.getSession().getAttribute( "loginId ");
DbtoFile dtf = new DbtoFile(pathTem + "\\Student.xls ",path + "/ " + finame);


if(dtf.writeToSheet(loginId.intValue()))
{

if (DownHistoryAction.downLoad(path,finame, "Student.xls ",response))
{
return mapping.findForward( "downOk ");
}
else
{
return (new ActionForward(mapping.getInput()));
}
}
else
{
return (new ActionForward(mapping.getInput()));
}
}
}


///////////////////////////////
struts 的
------解决方案--------------------
<a href= "abc.excel "> 导出文件 </a>
------解决方案--------------------
这个想法不太对吧,
在导出之前就确定文件路径?服务器并不能在客户端的硬盘上写文件。

应该是导出成excel之后,让浏览器提示用户保存到哪个位置,

------解决方案--------------------
将数据库的内容先一流的方式导出
然后
String fileName= "test.xls ";
response.setContentType( "application/x-msdownload ");
response.setHeader( "Content-Disposition ", "attachment; "+ " filename= " + new String(fileName.getBytes(), "UTF-8 "));
  相关解决方案