当前位置: 代码迷 >> Java Web开发 >> 大附件上载java.io.IOException: 拒绝访问
  详细解决方案

大附件上载java.io.IOException: 拒绝访问

热度:746   发布时间:2016-04-16 23:57:49.0
大附件下载java.io.IOException: 拒绝访问。
写在Servlet服务器的代码   
System.out.println("开始下载");
//    InputStream stream=request.getInputStream(); //锟斤拷取锟酵伙拷锟剿达拷锟酵癸拷4锟斤拷锟斤拷
//    String string=getReadToString(stream);
    System.out.println("stream="+string);
    JSONObject json=JSONObject.fromObject(string);
    InputStreamReader inputStreamReader =null;
    RandomAccessFile random = null;
    FileChannel channel = null;
    byte[] bytes=null;
    BufferedOutputStream outStream = null;
    ServletOutputStream sos = null;
    try {
//参数设值
        int startsSize=json.getInt("startPosition");
        int outSize=json.getInt("endPosition");
        String filepath=json.getString("downFileDir");

        StringBuffer buffer=null;

        random = new RandomAccessFile(filepath,"r");
        channel = random.getChannel();
        long fileSize = random.length();    //锟侥硷拷锟斤拷小
        //response.setContentType("application/pdf");
        filepath = new String(filepath.getBytes("gb2312"), "ISO8859-1");
    //设返回文件大小
           response.setContentLength((int) fileSize);
          bytes= new byte[(int)(outSize-startsSize)];
          MappedByteBuffer out =channel.map(MapMode.READ_ONLY,(startsSize),(outSize));
          out.get(bytes);     
          clean(out);//清空MappedByteBuffer句柄
          System.out.println(out+"out");
//向客户端输出流
          outStream = new BufferedOutputStream(response.getOutputStream());
          outStream.write(bytes,0,bytes.length);
          outStream.flush();
          outStream.close();
       writeJson(response, new String(bytes));    
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        try {
          if( channel != null){
            channel.close();
          }
          if (random != null) {
              random.close();
          }
          if (inputStreamReader != null) {
  相关解决方案