
org.apache.struts2.ServletActionContext.getResponse(ServletActionContext.java:130)
java.lang.NullPointerExceptionatorg.apache.struts2.ServletActionContext.getResponse(S...
java.lang.NullPointerException
at org.apache.struts2.ServletActionContext.getResponse(ServletActionContext.java:130)
at action.Test.downLoad(Test.java:119)
at action.Test.main(Test.java:55)
测试类
public static void main(String[] args) {
try {
downLoad("D:/TextImg/a.txt",true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void downLoad(String filePath, boolean isOnLine) throws Exception {
HttpServletResponse response=ServletActionContext.getResponse();
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) { // 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名应该编码成UTF-8
} else { // 纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
} 展开
at org.apache.struts2.ServletActionContext.getResponse(ServletActionContext.java:130)
at action.Test.downLoad(Test.java:119)
at action.Test.main(Test.java:55)
测试类
public static void main(String[] args) {
try {
downLoad("D:/TextImg/a.txt",true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void downLoad(String filePath, boolean isOnLine) throws Exception {
HttpServletResponse response=ServletActionContext.getResponse();
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) { // 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名应该编码成UTF-8
} else { // 纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
} 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
2个回答
展开全部
at action.Test.downLoad(Test.java:119)
错误发生在类Test的downLoad方法(Test类的119行)
你的119行那里错了
错误发生在类Test的downLoad方法(Test类的119行)
你的119行那里错了
追问
我知道啊就是HttpServletResponse response=ServletActionContext.getResponse();
报错啊,但他为什么报错。你觉得错在什么地方?原因呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询