大神们!上传文件出问题了, IndexOutOfBoundsException求帮忙

baidu_31938163 2016-10-20 02:48:16
上传文件在本地 window+tomcat是没问题的,结果上传到测试服务器linux+was就报错IndexOutOfBoundsException错误了是什么情况。
代码

// 创建根目录的保存变量
String rootPath = null;
// 文件名
String fileName = null;
// bytes
byte dataBytes[] = null;
// 声明文件读取类
DataInputStream in = null;
FileOutputStream fileOut = null;
try {
// 获取项目资源文件绝对路径
ServletContext application = request.getSession(true).getServletContext();
// 创建文件的保存目录
rootPath = application.getRealPath(File.separator + "ldjc" + File.separator + "resources" + File.separator + "upload");
// 取得客户端上传的数据类型
String contentType = request.getContentType();
if (contentType.indexOf("multipart/form-data") >= 0) {
// 读取上传的数据
in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
// 保存上传文件的数据
dataBytes = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
// 上传的数据保存在byte数组
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, 512);
totalBytesRead += byteRead;
}
// 根据byte数组创建字符串
String file = new String(dataBytes);
// System.out.println(file);
// 取得上传的数据的文件名
// String saveFile = file.substring(file.indexOf("filename=\"")
// + 10);
// saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
// saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,
// saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
// // 取得数据的分隔字符串
String boundary = contentType.substring(lastIndex + 1, contentType.length());
// 创建保存路径的文件名
fileName = rootPath + File.separator + "jyzc.xls";
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;

// 取得文件数据的开始的位置
int startPos = ((file.substring(0, pos)).getBytes()).length;

// 取得文件数据的结束的位置
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
// 检查上传文件的目录是否存在
File fileDir = new File(rootPath);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
// 创建文件的写出类
fileOut = new FileOutputStream(fileName);
// 保存文件的数据
fileOut.write(dataBytes, startPos, (endPos - startPos));

} else {
String content = request.getContentType();
result.append("上传的数据类型不是multipart/form-data");
return result.toString();
} catch (Exception e) {

} finally {
// 关闭资源
if (fileOut != null) {
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
fileOut = null;
}
}


异常

java.lang.IndexOutOfBoundsException: at java.io.FileOutputStream.writeBytes(Native Method) at java.io.FileOutputStream.write(FileOutputStream.java:290) at com.zh.ldjc.job.WljcJyzcHandler.uploadExcel(WljcJyzcHandler.java:241) at com.zh.ldjc.job.WljcJyzcHandler.processRequest(WljcJyzcHandler.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:618) at com.zh.ldjc.common.CoreHandler.doPost(CoreHandler.java:122) at javax.servlet.http.HttpServlet.service(HttpServlet.java:763) at javax.servlet.http.HttpServlet.service(HttpServlet.java:856) at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1213) at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:658) at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:526) at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3673) at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:269) at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:831) at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1478) at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:133) at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:457) at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:515) at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:300) at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:102) at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java:556) at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java:606) at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java:979) at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java:1064) at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1560)
...全文
587 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lijingzan 2016-10-24
  • 打赏
  • 举报
回复
看看是否上传了中文名字的文件?? 或者把读取的字节改小点看看
baidu_31938163 2016-10-24
  • 打赏
  • 举报
回复
引用 6 楼 bree06 的回复:
另外还要打印一下contentType, 我不记得contentType的内容还有=号的
不知道什么原因,是不是因为was和tomcat有差异?
baidu_31938163 2016-10-24
  • 打赏
  • 举报
回复
引用 6 楼 bree06 的回复:
另外还要打印一下contentType, 我不记得contentType的内容还有=号的
multipart/form-data; boundary=---------------------------7e017714e06f0 startPos 186,endPos 5941 这个是本地tomcat的 能运行 --------------------------------------------------------------------- contentType和上面一样的 startPos 108,endPos 10251 这个是在was运行的 报错了
bree06 2016-10-20
  • 打赏
  • 举报
回复
另外还要打印一下contentType, 我不记得contentType的内容还有=号的
bree06 2016-10-20
  • 打赏
  • 举报
回复
如果能确定64行 fileOut.write(dataBytes, startPos, (endPos - startPos)); 这个位置报错的话那应该是startPos=0且endPos-startPos=0或者endPos = dataBytes.length, 最好是打个log看看endPos和startPos
baidu_31938163 2016-10-20
  • 打赏
  • 举报
回复
up
baidu_31938163 2016-10-20
  • 打赏
  • 举报
回复
引用 2 楼 zhu19870712 的回复:
建议先打几个桩在测试服务器执行一下,那日志输出,找出问题所在
不行。。。测试服务器不在我们这里,测试很麻烦的,唉。都是传源码给他们 然后他们上传服务器的
0小黑0 2016-10-20
  • 打赏
  • 举报
回复
建议先打几个桩在测试服务器执行一下,那日志输出,找出问题所在
baidu_31938163 2016-10-20
  • 打赏
  • 举报
回复
64行 fileOut.write(dataBytes, startPos, (endPos - startPos)); 这个位置报错

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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