<form action="DownloadServlet" method="post" enctype="multipart/form-data" name="form2">
<table width=300 border="2" align="center">
<tr>
<td><input name="download" type="file" />
</td>
<td><input name="up" type="submit" value="下载" />
</td>
</tr>
</table>
</form>
通过选择,然后弹出一个文件选择的对话框,选择文件之后点击下载按钮后能下载
(servlet怎么写??????)jspservletdownload
<table width=300 border="2" align="center">
<tr>
<td><input name="download" type="file" />
</td>
<td><input name="up" type="submit" value="下载" />
</td>
</tr>
</table>
</form>
通过选择,然后弹出一个文件选择的对话框,选择文件之后点击下载按钮后能下载
(servlet怎么写??????)jspservletdownload
用FileInputStream读
ServletOutputStream out = null;
fis = new FileInputStream(file);
out = response.getOutputStream();
int n = 1024;
byte buffer[] = new byte[n];
while (fis.read(buffer, 0, n) != -1 && n > 0) {
out.write(buffer, 0, n);
}
out.flush();在Servlet的do***方法开始的时候加上response.addHeader("Content-Disposition","attachment;filename=" + fileurl); 这个就可以了大概把用的东西都给你粘出来了,剩下自己拼吧
* 下载文件
* @param filePath 文件路径(物理路径)
* @param fileName 源文件名称
*/
public void downLoadFile(String filePath, String fileName) {
File file = new File(filePath);
if (!file.exists() || file.isDirectory()) {
return;
}
InputStream input =null;
OutputStream output = null;
try {
input = new FileInputStream(file);
output = getResponse().getOutputStream();
getResponse().setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
byte[] buffer = new byte[1024];
int i = 0;
while ((i = input.read(buffer)) != -1) {
output.write(buffer, 0, i);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(null !=output) {
output.flush();
output.close();
}
if(null !=input) {
input.close();
}
} catch (Exception e) {}
}
}