下载的action代码如下:
[code="java"]
public ActionForward getAttachments(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
long id = (Long) request.getSession().getAttribute("mid");
UserInfo ui = (UserInfo) request.getSession().getAttribute("userinfo");
Attachment[] atts = mailService.getAttachments(ui, id);
response.reset();
for (int i = 0; i < atts.length; i++) {
response.addHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(atts[i].getFileName(), "utf-8"));
InputStream fis = atts[i].buildInputStream();
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
OutputStream toClient = new BufferedOutputStream(response
.getOutputStream());
String type = atts[i].getFileName();
int n = 0;
while (n != -1) {
n = type.indexOf(".");
type = type.substring(n + 1);
}
if (type.toUpperCase().equals("DOC"))
response.setContentType("application/msword");
else
response.setContentType("application/x-msdownload");
toClient.write(buffer);
toClient.flush();
toClient.close();
}
return null;
}
[/code]
每一个Attachment对应的一个附件:
[code="java"]
public class Attachment implements Serializable {
private static final long serialVersionUID = 7853572746765662317L;
private String fileName;
private byte[] data;
public Attachment() {
}
public Attachment(String fileName, byte[] data) {
this.fileName = fileName;
this.data = data;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public InputStream buildInputStream() {
return new ByteArrayInputStream(data);
}
}
[/code]
有多个文件下载的时候,[color=red]只能下载第一个,其余的就不弹出对话框下载[/color]了,另外在下载的时候[color=red]用IE自带的另存为可以下载,但是出来迅雷的时候不能下载,总是报long id = (Long) request.getSession().getAttribute("mid");空指针异常[/color]。这是什么原因呢?谢谢!
[b]问题补充:[/b]
上传没有问题。
[b]问题补充:[/b]
迅雷中[color=darkred]另存为为 taxmail.do,显示所需空间为0k,当报空指针异常的时候,它会一遍遍的产生多次[/color],另外,我为什么[color=red]不能下载多个文件呢?[/color]我的到的是多个文件,那个地方写错了么?
[b]问题补充:[/b]
我跟踪调试了一下,确实循环了三次,但是只下载了第一个,web难道不能这样一次下载多个文件?
呃,你的意思是同时一个线程同时下载多个文件吗?这是没办法做到的啊。。。。。不可能你下载一个文件,然后接二连三的几个文件自动一直下载的。。又不是病毒 :oops:
下载的时候都是一个个文件下载的,要多个文件,就压缩成一个rar下载。。呃。。难道是因为你这种做法而导致的下载问题。。。。
迅雷可能并不能解析你的下载地址,它没办法发起真正的下载请求。。这个可能要求你的系统要登陆有关系
你是不应该先检查一下上传机制是否有问题
空指针异常的话,可能request.getSession()是空的,迅雷会进行多线程下载,可能有的下载线程并没有登陆,而导致getSession()为空。
不可能一次下载多个文件。因为http无状态,下载一个文件后,就关闭当前连接,你要下载第二个要重新发起请求。我相信你在网上下东西,肯定没有一次下载2个东西下来的,多个文件都要压缩一下才能下