咔咔部里 2015-07-27 05:57 采纳率: 0%
浏览 1634

response图片下载...字节输出错误?

!图片下载错误请求不到是路径错了还是](https://img-ask.csdn.net/upload/201507/27/1437976537_395357.jpg)

 package cn.kakabuli.android.response;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 中文名文件下载
 */
public class Demo07 extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //服务器要求浏览器以下载方式打开资源
        String filename = "广州地铁线路图.jpg";
        //中文文件名要进行编码
        filename = URLEncoder.encode(filename, "UTF-8");
        response.setHeader("content-disposition", "attachment;filename="+filename);
        //以下代码就是用流的方式,将服务器的文件输出到浏览器中
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("../../images/广州地铁规划图.jpg");
        OutputStream os = response.getOutputStream();
        byte[] buf = new byte[2048];
        int len = 0;
        while((len=is.read(buf))>0){
            os.write(buf, 0, len);
        }
        os.close();
        is.close();
    }

}

download.html

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>download.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <style type="text/css">
        a:link{
            text-decoration:none
        }
        a:hover{
            text-decoration:underline;
            color:red   
        }
    </style>
  </head>
  <body>

    广州地铁图.jpg

    &nbsp;&nbsp;&nbsp;

    <!-- 
        /表示webapps目录
    -->
    <a href="/shulan-day07_request_response/Demo07">下载</a>

  </body>
</html>
  • 写回答

3条回答 默认 最新

  • 雪狸 2015-07-27 06:01
    关注

    http报500的错误,说明是服务器内部出现问题,而且报错信息为NullPointerException,在第35行出错,说明35行的位置,对象为空,应该是获取到的图片为空吧

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名