下载的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难道不能这样一次下载多个文件?