大神们!上传文件出问题了, IndexOutOfBoundsException求帮忙
上传文件在本地 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)
...全文
相关推荐
java 索引越界异常(IndexOutOfBoundsException)
Java中的“索引越界异常”(IndexOutOfBoundsException)是编程时常见的错误类型,尤其是在处理数组、集合或字符串等数据结构时。
IndexOutOfBoundsException如何解决.md
这个问题的根本原因在于索引值超出了集合元素的合法范围。例如,在数组或列表中,有效的索引范围是从0开始到元素数量减一的位置。
IndexOutOfBoundsException解决办法.md
在处理编程问题时,IndexOutOfBoundsException是一个常见的异常,通常发生在尝试访问数组、列表或其他集合类型的元素时,使用了超出其合法范围的索引值。
IndexOutOfBoundsException(解决方案).md
这种方法是在异常已经发生时的应对措施,虽然可以解决问题,但可能会导致额外的性能开销。第三,对于Java 8及以上版本,可以利用Optional类来避免异常的抛出。
IndexOutOfBoundsException.md
有效范围检查是预防数组越界异常的首要步骤,因为这样可以在问题发生之前予以避免。其次,可以使用默认值或者边界处理机制来应对可能的索引越界情况。


