关于导出任意文件类型

本文提供了一个使用Java Servlet实现文件下载的例子,包括如何设置HTTP响应头以支持中文文件名及利用SmartUpload组件进行文件下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

好久没有写点东西了,其实自己已经没有什么好写的,技术那些东西,自己还学到的很少很少,经验是不断积累起来的,还有就是靠自己不断的追求吧.


(其实就是上次写导出文件的一个补充罢了,唯一变的地方其实就是一个设定问题,)也就是response.setContentType("application/x-msdownload;charset=UTF-8");


呵呵,我写的都是一些小东西.不过自己还是尽力将之做得更好.其他不说了,直接来程序吧.

Java代码,这个是一个Servlet,其实无所谓,你在页面上直接location.href也可.里面有不少无用代码,不过还是留下吧,呵呵,有些其实也挺有用的.



import java.io.IOException;

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

import org.lxh.smart.SmartUpload;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.radiantek.m2m.doc.dao.DownloadDocDao;

public class DownLoadDoc extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;

public DownLoadDoc() {
super();
}

public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

//重点
response.setContentType("application/x-msdownload;charset=UTF-8");

// 这里得到的是ID,然后再去找.
String FileName = new String(request.getParameter("downName"));
// String FilePath = new String(
// request.getParameter("FilePath").getBytes("iso8859_1") ) ;
String FilePath = request.getSession().getServletContext().getRealPath(
"/");

String appXmlPath = "/com/radiantek/m2m/config/spring/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
appXmlPath);
DownloadDocDao downloadDocDao = (DownloadDocDao) applicationContext
.getBean("downloadDocDao");
String fileName = downloadDocDao.findByFileName(FileName);

// System.out.println(FilePath);
// @SuppressWarnings("unused")
// DownLoad dl = new DownLoad () ;

try {
// dl.down(response, FilePath, FileName);
// System.out.println(FilePath) ;
SmartUpload smart = new SmartUpload();
smart.initialize(super.getServletConfig(), request, response);
smart.setContentDisposition(null);

FilePath = fileName;
int filen = FilePath.lastIndexOf("\\");

String fName = FilePath.substring(filen+1);

// System.out.println(FilePath);

//重点,最主要的就是这句话了,
smart.downloadFile(FilePath, "application/x-msdownload;charset=UTF-8", java.net.URLEncoder.encode(fName, "UTF-8"));

} catch (Exception e) {

e.printStackTrace();
}
}

public void init() throws ServletException {
}

}




JSP页面代码


....这里就是一个传递参数问题,用到了Struts的一些内容,不管用什么都可以,只要传递参数了.
<input type="button" value=" 下载 " class="button_class"
onclick="down('<s:property value="FILE_ID"/>');">

.....
function down(DOWN_NAME){

location.href = "<%=path%>/servlet/DownLoadDoc?downName=" + DOWN_NAME;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值