javaweb下载的代码怎么写

来源:百度知道 编辑:UC知道 时间:2024/05/06 05:09:33
想知道写个下载的web页面,如何实现文件传输,不是很了解,希望高手指点下.

这个是用spSmartUpload实现的 使用的时候导入smartupload的jar包

jsp页面

<body>
<%
File f = new File("e:\\abc\\");
String[] fnames = f.list();
for(int i = 0; i < fnames.length; i++){

%>
<a href="down?fname=<%=fnames[i]%>"><%=fnames[i]%></a><br>
<%}%>
</body>

servlet中

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fname = new String(request.getParameter("fname").getBytes(
"iso-8859-1"));

SmartUpload su = new SmartUpload();

su.initialize(getServletConfig(), request, response);// 初始化
try {
su.setContentDisposition(null);//出现下载提示
su.downloadFile("e:\\abc\\" + fname);
} catch (SmartUploadE