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

A_S478 2013-01-22 07:18:51
写在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) {
inputStreamReader.close();
}
if( outStream != null ) outStream.close();

if(sos != null) sos.close();

} catch (Exception e) {
e.printStackTrace();
}
}
...全文
459 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
A_S478 2013-03-01
  • 打赏
  • 举报
回复
引用 8 楼 wapigzhu 的回复:
应该不是权限问题,权限问题的话 random = new RandomAccessFile(filepath,"r"); 这句就该报错了 试了下 MappedByteBuffer out =channel.map(MapMode.READ_ONLY,(startsSize),(outSize)); 如果startsSize + outSize > fileSize了 倒是会报拒绝访……
果然是因为这个问题,参数理解错了,惭愧…… 谢谢了
wapigzhu 2013-01-23
  • 打赏
  • 举报
回复
应该不是权限问题,权限问题的话 random = new RandomAccessFile(filepath,"r"); 这句就该报错了 试了下 MappedByteBuffer out =channel.map(MapMode.READ_ONLY,(startsSize),(outSize)); 如果startsSize + outSize > fileSize了 倒是会报拒绝访问,你可以检查下这3个参数看
yzsunlight 2013-01-23
  • 打赏
  • 举报
回复
小文件 有没试过 有没问题!
  • 打赏
  • 举报
回复
你每次读取的大小设置成了多少?
vision2000 2013-01-23
  • 打赏
  • 举报
回复
文件是否有权限读取?
Kimihiro_233 2013-01-23
  • 打赏
  • 举报
回复
虽然看不懂,帮顶
A_S478 2013-01-23
  • 打赏
  • 举报
回复
太凄凉了吧,灌个水的都没有
A_S478 2013-01-22
  • 打赏
  • 举报
回复
出错行是:MappedByteBuffer out =channel.map(MapMode.READ_ONLY,(startsSize),(outSize)); 求大神指点
A_S478 2013-01-22
  • 打赏
  • 举报
回复

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧