下载的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");空指针异常。这是什么原因呢?谢谢!
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");空指针异常。这是什么原因呢?谢谢!
解决方案 »
- 263通信 明天面试有以前面过的同学吗?分享一下经验啊!!!
- 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 linux下启动weblogic报错
- 跪求:[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
- 跪求代码Jsp或者Java导出数据表到excel中
- 关于分页的疑问,请求兄弟姐妹一起来分析下?
- sevlet中读取session的问题,在线等
- 如何读取这个XML文件??
- 关于<jsp:param name="actionParam" value="查看所有经理" />中传的值得乱码问题,搞了一晚上没搞出来!救救我巴!!!
- 郁闷呀,大家帮帮我看看这个问题用JAVA如何实现
- tomcat deploy问题
- struts 2.0中 action提交报错javax.servlet.ServletException!
- 显示内容一般正常一般是口口口
说明有个对象是null,但是你又调用其方法。
从这句里看,应该是session问题,你可以查查。
request.getSession()得到null
同一个ID同一个UI ,那你的ATTS的值不一样??