如何通过multipart/form-data将图像上传到j2me中的服务器?
由于我是 j2me 的新手,我正在尝试从 j2me 中的移动设备上传图像。它在服务器端显示异常。
org.apache.commons.fileupload.FileUploadException: Processing of ``multipart/form-
data request failed. Stream ended unexpectedly
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadB
ase.java:384)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(
ServletFileUpload.java:116)
at org.apache.jsp.photo_jsp._jspService(photo_jsp.java:119)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:384)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:3
20)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke
(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke
(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke
(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke
(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process
(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol
$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run
(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)
请建议我如何解决。
在 J2me 中,我使用代码将图像上传到服务器。
HttpConnection conn = null;
OutputStream os = null;
// InputStream is = null;
try
{
System.out.println("url:" + serverurl);
conn = (HttpConnection)Connector.open(serverurl);
conn.setRequestMethod(HttpConnection.POST);
// String postData = "";
InputStream imgIs = getClass().getResourceAsStream(FILE);
byte []imgData = new byte[imgIs.available()];
imgIs.read(imgData);
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");
os = conn.openOutputStream();
// os.write(message1.getBytes());
// For First Image uploading
String message1 = "";
message1 += "-----------------------------4664151417711" + CrLf;
message1 += "Content-Disposition: form-data; name=\"photo\";
filename=\"" + FILE + "\"" + CrLf;
message1 += "Content-Type: image/jpeg" + CrLf; message1 += CrLf;
os.write(message1.getBytes());
// SEND THE IMAGE
int index = 0;
int size = 1024;
do{
System.out.println("write:" + index);
if((index+size)>imgData.length){
size = imgData.length - index;
}
os.write(imgData, index, size);
index+=size;
progress(imgData.length, index); // update the progress bar.
}while(index<imgData.length);
System.out.println("DONE");
}
catch(Exception e)
{
e.printStackTrace();
}finally
{
System.out.println("Close connection");
try
{
//os.close();
}
catch(Exception e){}
try
{
// is.close();
}
catch(Exception e){}
try
{
// conn.close();
}
catch(Exception e){}
}
on serside we use the code:
System.out.println(".............Images..122223...Images...");
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
System.out.println(".............Images...11111..test..."+upload);
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
System.out.println(".............Images...11111..test..."+isMultipart);
if(isMultipart)
{
System.out.println(".............Images...11111..Images..Images....");
List items = upload.parseRequest(request);
System.out.println(".............Images.33333....Images..Images....");
Iterator iterator = items.iterator();
while(iterator.hasNext())
{
String fieldName = "";
String fieldValue="";
String fileName = "";
FileItem fileItem=(FileItem)iterator.next();
System.out.println(fileItem.isFormField());
if (fileItem.isFormField())
{
fieldName = fileItem.getFieldName();
fieldValue = fileItem.getString("UTF-8").trim();
if(fieldName.equals("photo"))
{
if(fieldValue!=null && fieldValue!="")
{
photo = fieldValue;
System.out.println(photo);
}
}
}
else
try
{
if(fileItem != null)
{
fileName = applicationNumber+"_"+fileItem.getName();
fieldName = fileItem.getFieldName();
System.out.println("applicationNumber="+fileName);
}
if(fieldName.equals("photo"))
{
String x=fileName;
if(x.indexOf("/")!=-1)
{
uploadStr = x.substring(0,x.lastIndexOf("/") );
UploadFile = x.substring(x.lastIndexOf("/")+1,x.length() );
}
long sizeInBytes = fileItem.getSize();
String applicationURL = "C:/Program Files/
Apache Software Foundation/Tomcat 6.0/webapps/apply_smart/applications";
StringTokenizer stringTokenizer = new StringTokenizer(UploadFile,"\\");
int count=stringTokenizer.countTokens();
for(int i=1;i<=count;i++)
{
String token=stringTokenizer.nextToken();
if(i==count)
{
audioFileName=token;
}
}
applicationURL = applicationURL;
System.out.println("Path="+applicationURL);
String[] filePath = fileName.split("/");
uploadedFile = new File(applicationURL+"/"+filePath[filePath.length - 1]);
long size=uploadedFile.length();
if(size>=11264)
{
}
try
{
fileItem.write(uploadedFile);
path = applicationURL.replaceAll
("C:/ProgramFiles/ApacheSoftwareFoundation /Tomcat 6.0/ webapps/apply_smart/", "");
path= path + "/"+ filePath[filePath.length - 1];
System.out.println("path::::::"+path);
}
catch (java.lang.Exception e)
{
out.println(e);
}
}
}//else
}
catch(Exception exe)
{
out.println(exe);
break;
}
}
请建议我如何解决异常并将图像发送到服务器。
提前致谢。 特贾。
As I am new to j2me,I am trying to upload image from mobile in j2me.It is showing exception in server side.
org.apache.commons.fileupload.FileUploadException: Processing of ``multipart/form-
data request failed. Stream ended unexpectedly
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadB
ase.java:384)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(
ServletFileUpload.java:116)
at org.apache.jsp.photo_jsp._jspService(photo_jsp.java:119)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:384)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:3
20)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke
(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke
(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke
(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke
(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process
(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol
$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run
(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)
please suggest me how to solve.
From J2me I am using code to upload image to server is.
HttpConnection conn = null;
OutputStream os = null;
// InputStream is = null;
try
{
System.out.println("url:" + serverurl);
conn = (HttpConnection)Connector.open(serverurl);
conn.setRequestMethod(HttpConnection.POST);
// String postData = "";
InputStream imgIs = getClass().getResourceAsStream(FILE);
byte []imgData = new byte[imgIs.available()];
imgIs.read(imgData);
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");
os = conn.openOutputStream();
// os.write(message1.getBytes());
// For First Image uploading
String message1 = "";
message1 += "-----------------------------4664151417711" + CrLf;
message1 += "Content-Disposition: form-data; name=\"photo\";
filename=\"" + FILE + "\"" + CrLf;
message1 += "Content-Type: image/jpeg" + CrLf; message1 += CrLf;
os.write(message1.getBytes());
// SEND THE IMAGE
int index = 0;
int size = 1024;
do{
System.out.println("write:" + index);
if((index+size)>imgData.length){
size = imgData.length - index;
}
os.write(imgData, index, size);
index+=size;
progress(imgData.length, index); // update the progress bar.
}while(index<imgData.length);
System.out.println("DONE");
}
catch(Exception e)
{
e.printStackTrace();
}finally
{
System.out.println("Close connection");
try
{
//os.close();
}
catch(Exception e){}
try
{
// is.close();
}
catch(Exception e){}
try
{
// conn.close();
}
catch(Exception e){}
}
on serside we use the code:
System.out.println(".............Images..122223...Images...");
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
System.out.println(".............Images...11111..test..."+upload);
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
System.out.println(".............Images...11111..test..."+isMultipart);
if(isMultipart)
{
System.out.println(".............Images...11111..Images..Images....");
List items = upload.parseRequest(request);
System.out.println(".............Images.33333....Images..Images....");
Iterator iterator = items.iterator();
while(iterator.hasNext())
{
String fieldName = "";
String fieldValue="";
String fileName = "";
FileItem fileItem=(FileItem)iterator.next();
System.out.println(fileItem.isFormField());
if (fileItem.isFormField())
{
fieldName = fileItem.getFieldName();
fieldValue = fileItem.getString("UTF-8").trim();
if(fieldName.equals("photo"))
{
if(fieldValue!=null && fieldValue!="")
{
photo = fieldValue;
System.out.println(photo);
}
}
}
else
try
{
if(fileItem != null)
{
fileName = applicationNumber+"_"+fileItem.getName();
fieldName = fileItem.getFieldName();
System.out.println("applicationNumber="+fileName);
}
if(fieldName.equals("photo"))
{
String x=fileName;
if(x.indexOf("/")!=-1)
{
uploadStr = x.substring(0,x.lastIndexOf("/") );
UploadFile = x.substring(x.lastIndexOf("/")+1,x.length() );
}
long sizeInBytes = fileItem.getSize();
String applicationURL = "C:/Program Files/
Apache Software Foundation/Tomcat 6.0/webapps/apply_smart/applications";
StringTokenizer stringTokenizer = new StringTokenizer(UploadFile,"\\");
int count=stringTokenizer.countTokens();
for(int i=1;i<=count;i++)
{
String token=stringTokenizer.nextToken();
if(i==count)
{
audioFileName=token;
}
}
applicationURL = applicationURL;
System.out.println("Path="+applicationURL);
String[] filePath = fileName.split("/");
uploadedFile = new File(applicationURL+"/"+filePath[filePath.length - 1]);
long size=uploadedFile.length();
if(size>=11264)
{
}
try
{
fileItem.write(uploadedFile);
path = applicationURL.replaceAll
("C:/ProgramFiles/ApacheSoftwareFoundation /Tomcat 6.0/ webapps/apply_smart/", "");
path= path + "/"+ filePath[filePath.length - 1];
System.out.println("path::::::"+path);
}
catch (java.lang.Exception e)
{
out.println(e);
}
}
}//else
}
catch(Exception exe)
{
out.println(exe);
break;
}
}
Please suggest me how to solve the exception and send image to server.
Thanks in advance.
Teja.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,我没有时间检查您的代码以查看问题所在,但我之前使用过 Jason 的 解决方案伍兹
Sorry I do not have time to check over your code to see what is wrong, but I used previously this solution by Jason Woods