java编写的下载程序无法显示图片 求大神指点 (详解)
代码如下:packagecom.servelttest;importjava.io.FileInputStream;importjava.io.IOException;i...
代码如下:
package com.servelttest;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileDownLoad extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
//System.out.println("<h1>fveveve!</h1>");
//System.out.println("<br/>");
try {
PrintWriter out = response.getWriter();
//演示下载文件
//告诉浏览器有东西要下载
response.setHeader("Content-Disposition","attachment;filename=1.jpg");
//获取要下载的文件的全路径
//String path=this.getServletContext().getRealPath("/images/5.png");
//String path=this.getServletContext().getResourcePaths("/images/5.png");
InputStream in = this.getServletContext().getResourceAsStream("/images/1.jpg");
//创建文件输入流
//FileInputStream fis=new FileInputStream(path);
//做一个缓冲字节数组
byte buff[]=new byte[1024];
int len=0;
OutputStream os=response.getOutputStream();
while((len=in.read(buff))>0){
os.write(buff, 0, len);
}
os.close();
in.close();
} catch (Exception e) {
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
} 展开
package com.servelttest;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileDownLoad extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
//System.out.println("<h1>fveveve!</h1>");
//System.out.println("<br/>");
try {
PrintWriter out = response.getWriter();
//演示下载文件
//告诉浏览器有东西要下载
response.setHeader("Content-Disposition","attachment;filename=1.jpg");
//获取要下载的文件的全路径
//String path=this.getServletContext().getRealPath("/images/5.png");
//String path=this.getServletContext().getResourcePaths("/images/5.png");
InputStream in = this.getServletContext().getResourceAsStream("/images/1.jpg");
//创建文件输入流
//FileInputStream fis=new FileInputStream(path);
//做一个缓冲字节数组
byte buff[]=new byte[1024];
int len=0;
OutputStream os=response.getOutputStream();
while((len=in.read(buff))>0){
os.write(buff, 0, len);
}
os.close();
in.close();
} catch (Exception e) {
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
} 展开
1个回答
2013-08-12
展开全部
“因为该文件是空的”
代码应该是正确的,而可能是文件的问题
} catch (Exception e) {
错误捕捉了,不要不打印错误信息!有意外之后,一定要让自己知道是出什么问题了。
代码应该是正确的,而可能是文件的问题
} catch (Exception e) {
错误捕捉了,不要不打印错误信息!有意外之后,一定要让自己知道是出什么问题了。
追问
你好 文件没问题 最后改了代码 就好了 谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |