我知道可以在HTML的页面上通过:
[code="java"][/code]
后台框架
[code="java"][/code]
来实现这个avi文件的下载。
<注:upfile为我webRoot目录下的资源文件夹>
那么怎么才能把这个改为在servlet中实现呢?
即:通过点击表单按钮:
[code="java"][/code]
[code="java"][/code]
然后如何在servlet中实现直接跳转到该链接实现下载?因为我不想用流下载,所以想直接在servlet中跳转。
[code="java"]
request.getRequestDispatcher("\upfile\10_后台框架.avi").forward(request,response);
[/code]
那你就直接用request的跳转到”10_后台框架.avi“相对web根目录所在的位置看看
String headName = "A.txt";//文件名
String fileUrl = "F:/company/heat energy/temp/A.txt";//文件路径
if (headName.endsWith(".jpg") || (headName.endsWith(".gif"))) {
} else if (headName.endsWith(".txt")) {
response.setContentType("text;charset=GB2312");
response.setHeader("Content-disposition", "attachment; filename="
+ headName);
} else {
response.setContentType("application/doc;charset=GB2312");
response.setHeader("Content-disposition", "attachment; filename="
+ headName);
}
String fileURL = fileUrl;
if (fileURL == null)
return;
try {
File file = new File(fileUrl);
FileInputStream bis = new FileInputStream(file);
OutputStream bos = response.getOutputStream();
byte[] buff = new byte[1024];
int readCount = 0;
int i = 0;
readCount = bis.read(buff);
while (readCount != -1) {
bos.write(buff, 0, readCount);
readCount = bis.read(buff);
}
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (Exception e) {
e.printStackTrace();
}