JAVA跨服务器上传文件失败,当尝试上传服务器上的文件时发生java.lang.IndexOutOfBoundsException...

错误:

java.lang.IndexOutOfBoundsException

java.io.FileOutputStream.writeBytes(Native Method)

java.io.FileOutputStream.write(FileOutputStream.java:297)

com.ninenexus.simplesign.storeanddisplay.StoreAndDisplayImage.doPost(StoreAndDisplayImage.java:85)

javax.servlet.http.HttpServlet.service(HttpServlet.java:754)

javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

代码:

String contentType = request.getContentType();

if ((contentType != null)

&& (contentType.indexOf("multipart/form-data") >= 0))

{

DataInputStream in = new DataInputStream(request.getInputStream());

int formDataLength = request.getContentLength();

byte dataBytes[] = new byte[formDataLength];

int byteRead = 0;

int totalBytesRead = 0;

while (totalBytesRead < formDataLength)

{

byteRead = in.read(dataBytes, totalBytesRead, formDataLength);

totalBytesRead += byteRead;

}

String file = new String(dataBytes);

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());

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;

saveFile = "d:/" + saveFile;

try

{

File f = new File(saveFile);

FileOutputStream fileOut = new FileOutputStream(f);

fileOut.write(dataBytes, startPos, (endPos - startPos)); // Line 85

fileOut.flush();

fileOut.close();

}

catch(...)

{

...

}

}

else

{

...

}

线的输出无。 85 is

dataBytes=[[email protected], startPos=142, (endPos - startPos)=75944.

我想从用户通过输入type ='文件',然后将其保存在使用此代码的目录中的文件。它在tomcat服务器上正常工作。文件被保存在目录中。但是当我创建战争文件并将其上传到服务器上时。尝试在主服务器上运行此代码时,出现此错误。没有解决这个问题的线索。帮助真的很感激。

+0

你能打印'dataBytes.length'吗? –

+0

'dataBytes.length = 76132' –

+1

@ shabbir.rang可以肯定的是,这些数字来自您的主服务器(您从哪里得到错误)? –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值