Android将图像上传到jsp页面
我想从我的android应用程序上传图像到jsp服务器的页面,该页面将处理图像并将其保存到服务器中。但在调试时,我得到了Android将图像上传到jsp页面,android,jsp,java-server,Android,Jsp,Java Server,我想从我的android应用程序上传图像到jsp服务器的页面,该页面将处理图像并将其保存到服务器中。但在调试时,我得到了java.lang.IndexOutOfBoundsException。这是我在jsp页面中的代码 String contentType = request.getContentType(); //here we are checking the content type is not equal to Null and as well as the passed da
java.lang.IndexOutOfBoundsException
。这是我在jsp页面中的代码
String contentType = request.getContentType();
//here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
InputStream in = request.getPart("Image").getInputStream();
//DataInputStream in = new DataInputStream(request.getInputStream());
//we are taking the length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
//for saving the file name
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());
int pos;
//extracting the index of file
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;
// creating a new file with the same name and writing the content in new file
FileOutputStream fileOut = new FileOutputStream(getServletContext().getRealPath("/")+"images/"+saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
String contentType=request.getContentType();
//这里我们检查内容类型是否不等于Null,以及从mulitpart/表单数据传递的数据是否大于或等于0
if((contentType!=null)&&(contentType.indexOf(“多部分/表单数据”)>=0)){
InputStream in=request.getPart(“图像”).getInputStream();
//DataInputStream in=新的DataInputStream(request.getInputStream());
//我们正在计算内容类型数据的长度
int formDataLength=request.getContentLength();
字节数据字节[]=新字节[formDataLength];
int byteRead=0;
int totalBytesRead=0;
//此循环将上载的文件转换为字节码
while(totalBytesRead
我怎样才能解决这个问题
以下是错误日志:
>目标VM中发生异常:
java.lang.IndexOutOfBoundsException:
在java.io.BufferedInputStream.read处(BufferedInputStream.java:327)
位于org.apache.felix.gogo.runtime.threadio.ThreadInputStream.read(ThreadInputStream.java:87)
在org.apache.jsp.upload\u jsp.\u jsp服务(upload\u jsp.java:64)
位于org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
位于javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
位于org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:403)
位于org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:492)
位于org.apache.jasper.servlet.JspServlet.service(JspServlet.java:378)
位于javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
位于org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
位于org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
位于org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
位于org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
位于org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
位于com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
位于com.sun.enterprise.web.peSessionLockingsStandardPipeline.invoke(peSessionLockingsStandardPipeline.java:91)
位于org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
位于org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
位于org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
位于com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
位于com.sun.grizzly.http.ProcessorTask.InvokeApter(ProcessorTask.java:828)
在com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
位于com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
在com.sun.grizzly.http.DefaultProtocolFilter.execute上(DefaultProtocolFilter.java:225)
位于com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)上
在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)上
http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
位于com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
在com.sun.grizzly.SelectionKeyContextTask.call上(SelectionKeyContextTask.java:59)
位于com.sun.grizzly.ContextTask.run(ContextTask.java:71)
位于com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
位于com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
运行(Thread.java:722)
您应该使用Apache API来上传文件,或者您可以使用Servlet 3.0-Part
API。为什么不使用诸如?抱歉!我的声誉不足以让我现在发布错误日志,我将在您使用indexOf()后发布它
在代码中的许多地方。其中任何一个都可能引发此异常。这不是一个好的做法。而是使用Apache Commons FileUpload。Hardik,你能给我举一个Apache Commons FileUpload的例子吗?