下载的action代码如下: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;
}每一个Attachment对应的一个附件: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);
    }
}有多个文件下载的时候,只能下载第一个,其余的就不弹出对话框下载了,另外在下载的时候用IE自带的另存为可以下载,但是出来迅雷的时候不能下载,总是报long id = (Long) request.getSession().getAttribute("mid");空指针异常。这是什么原因呢?谢谢!

解决方案 »

  1.   

    总是报long id = (Long) request.getSession().getAttribute("mid");空指针异常。
    说明有个对象是null,但是你又调用其方法。
    从这句里看,应该是session问题,你可以查查。
    request.getSession()得到null
      

  2.   

    这一个如果用另存为方式保存文件的话,是没问题的,session是正确的,用迅雷的时候,他下面的显示的另存为:taxmail.do而所需空间为0k,然后会一遍一遍的报空指针异常。
      

  3.   

    我跟踪调试了一下,确实循环了三次,但是只下载了第一个,web难道不能这样一次下载多个文件? 
      

  4.   

    http://download.csdn.net/source/1119779解决,多给点分,谢谢
      

  5.   


    同一个ID同一个UI ,那你的ATTS的值不一样??