legend_jhz 2009-03-18 15:17
浏览 156
已采纳

struts1 多个文件下载问题

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

  • 写回答

4条回答

  • playfish05 2009-03-18 16:48
    关注

    呃,你的意思是同时一个线程同时下载多个文件吗?这是没办法做到的啊。。。。。不可能你下载一个文件,然后接二连三的几个文件自动一直下载的。。又不是病毒 :oops:

    下载的时候都是一个个文件下载的,要多个文件,就压缩成一个rar下载。。呃。。难道是因为你这种做法而导致的下载问题。。。。

    迅雷可能并不能解析你的下载地址,它没办法发起真正的下载请求。。这个可能要求你的系统要登陆有关系

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?