http://developer.nokia.com/Community/Wiki/HTTP_Post_multipart_file_upload_in_Java_ME
Here is a J2ME class to handle file uploads via HTTP POST Multipart Requests.
Source Code: HttpMultipartRequest class
import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.Hashtable; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; public class HttpMultipartRequest { static final String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy"; byte[] postBytes = null; String url = null; public HttpMultipartRequest(String url, Hashtable params, String fileField, String fileName, String fileType, byte[] fileBytes) throws Exception { this.url = url; String boundary = getBoundaryString(); String boundaryMessage = getBoundaryMessage(boundary, params, fileField, fileName, fileType); String endBoundary = "\r\n--" + boundary + "--\r\n"; ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(boundaryMessage.getBytes()); bos.write(fileBytes); bos.write(endBoundary.getBytes()); this.postBytes = bos.toByteArray(); bos.close(); } String getBoundaryString() { return BOUNDARY; } String getBoundaryMessage(String boundary, Hashtable params, String fileField, String fileName, String fileType) { StringBuffer res = new StringBuffer("--").append(boundary).append("\r\n"); Enumeration keys = params.keys(); while(keys.hasMoreElements()) { String key = (String)keys.nextElement(); String value = (String)params.get(key); res.append("Content-Disposition: form-data; name=\"").append(key).append("\"\r\n") .append("\r\n").append(value).append("\r\n") .append("--").append(boundary).append("\r\n"); } res.append("Content-Disposition: form-data; name=\"").append(fileField).append("\"; filename=\"").append(fileName).append("\"\r\n") .append("Content-Type: ").append(fileType).append("\r\n\r\n"); return res.toString(); } public byte[] send() throws Exception { HttpConnection hc = null; InputStream is = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] res = null; try { hc = (HttpConnection) Connector.open(url); hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + getBoundaryString()); hc.setRequestMethod(HttpConnection.POST); OutputStream dout = hc.openOutputStream(); dout.write(postBytes); dout.close(); int ch; is = hc.openInputStream(); while ((ch = is.read()) != -1) { bos.write(ch); } res = bos.toByteArray(); } catch(Exception e) { e.printStackTrace(); } finally { try { if(bos != null) bos.close(); if(is != null) is.close(); if(hc != null) hc.close(); } catch(Exception e2) { e2.printStackTrace(); } } return res; } }
Sample usage
Here's a code snippet to upload a file via HttpMultipartRequest class:
byte[] fileBytes = getFileBytes(); //retrieve file bytes with your own code Hashtable params = new Hashtable(); params.put("custom_param", "param_value"); params.put("custom_param2", "param_value2"); HttpMultipartRequest req = new HttpMultipartRequest( "http://www.server.com/uploadScript.php", params, "upload_field", "original_filename.png", "image/png", fileBytes ); byte[] response = req.send();
Sample server code (PHP)
This is a sample PHP script that handles the upload. It doesn't actually save the uploaded file, but only displays some infos about the upload size and parameters.
<?php $filesize = filesize($_FILES['upload_field']['tmp_name']); echo "The uploaded file size is " . $filesize . " bytes\n"; foreach($_POST as $key => $value) { echo "Parameter name: " . $key . ", value: " . $value . "\n"; } ?>
Comments
this piece of code works great, but i am having a little problem the HttpConnection its not Setting the Content-Lenght and the transfer its being made with transfer-encodign: chunked
i dont know if tha later is giving problems but the thing about the content-lenght its giving some problems with a server with mod_sec that its forbiding REQUEST without Content-Lenght header so im getting a 403 its there any way to set the content-Lenght? setRequestParameter() i think is not working thanks in adavance
Maximum file size that can be uploaded from mobile
I tried the same example. I am able to upload file which are less than 150 bytes. If I upload the file more than that size I get the following error. Please help me in this
java.io.IOException: error 104 during TCP read
at com.sun.midp.io.j2me.socket.Protocol.nonBufferedRead(Protocol.java:299) at com.sun.midp.io.BufferedConnectionAdapter.readBytes(BufferedConnectionAdapter.java:99) at com.sun.midp.io.BaseInputStream.read(ConnectionBaseAdapter.java:582) at com.sun.midp.io.BaseInputStream.read(ConnectionBaseAdapter.java:511) at java.io.DataInputStream.read(+7) at com.sun.midp.io.j2me.http.Protocol.readLine(+4) at com.sun.midp.io.j2me.http.Protocol.readResponseMessage(+17) at com.sun.midp.io.j2me.http.Protocol.finishRequestGetResponseHeader(+39) at com.sun.midp.io.j2me.http.Protocol.sendRequest(+50) at com.sun.midp.io.j2me.http.Protocol.sendRequest(+6) at com.sun.midp.io.j2me.http.Protocol.closeOutputStream(+4) at com.sun.midp.io.BaseOutputStream.close(ConnectionBaseAdapter.java:737) at hello.HttpMultipartRequest.send(HttpMultipartRequest.java:91) at hello.PostMIDlet.run(PostMIDlet.java:104)
Thanks in advance Smith,
Nokia 6300 and J2ME problems
I made MIDDLET which is capable of working to Semens CX75. (Very old!) My MIDDLET create photo and send it to site by HTTP. But "Nokia 6300" reject my MIDDLET.
Help me!
(1) I find sample: HTTP Post multipart file upload with J2ME HTTP Post multipart file upload with J2ME But it is not an example. It is a hint. It cannot be compiled!
Show to us an example which is compiled by WTK2.5.2 and carried out by Nokia S40 devices.
(2) Show to us an prescription which should compile WTK2.5.2 MIDDLETs examples for Nokia S40 devices. (In particular "CameraDemo" and HttpConnection-samples.)
Away123 - Upload Just only 1 KB
Can i use this class for upload file bigger than 1 KB ? Please help me..
相关推荐
本篇文章将深入探讨如何利用jQuery File Upload库,结合Java后端,实现高效、友好的文件上传功能。 jQuery File Upload是一款功能强大的前端文件上传组件,它支持多文件选择、拖放操作、进度条显示以及预览功能,极...
在这个主题中,"ASP.NET Web API File Upload and Multipart MIME.rar" 提到的是关于在ASP.NET Web API中处理文件上传以及如何利用MIME多部分格式来实现这一功能。 1. **MIME多部分格式**: MIME(Multipurpose ...
### 解决Java enctype "multipart/form-data" 文件上传传值问题 在Java Web开发中,处理文件上传是一项常见的任务。特别是当涉及到使用`multipart/form-data`作为表单的编码类型时,这种需求更为突出。本文将深入...
"jQuery-File-Upload" 是一个著名的前端文件上传插件,尤其在Web开发领域中广泛应用。这个插件基于JavaScript库jQuery,提供了强大的功能,如多文件选择、文件预览、进度条显示、取消上传以及大文件分块上传等。在这...
<form action="upload.jsp" method="post" enctype="multipart/form-data"> <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="Upload File" name="submit"> ``` 这里的`...
在Java Web开发中,我们经常遇到表单提交数据的情况,特别是涉及到文件上传时,`<form>`标签的`ENCTYPE`属性通常会被设置为`multipart/form-data`。这是因为`multipart/form-data`允许在请求中携带二进制数据,如...
<form id="fileupload" action="/Home/Upload" method="post" enctype="multipart/form-data"> <input type="file" name="files" id="files" multiple> ``` ```javascript $("#fileupload").fileupload({ url: '/...
`java upload file demo` 涉及到的关键知识点主要包括以下几个方面: 1. **Servlet**:Java Web开发中的核心组件,用于处理HTTP请求和响应。文件上传通常发生在HTTP POST请求中,Servlet是接收和处理这些请求的主要...
标题"file in android upload to web server"指向的是使用Android API与Web服务器进行交互,将本地文件作为数据上传的过程。 1. **使用HttpURLConnection**:这是Android SDK内建的一种网络通信方式。首先,你需要...
<form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> 上传 ``` 5. **错误处理**:在处理文件上传时,要考虑到可能出现的错误,如文件大小限制、非法文件...
2. **设置请求头**:文件上传需要在POST请求中包含Content-Type头,通常为`multipart/form-data`。这告诉服务器请求体包含了要上传的文件内容。还需要指定Content-Length头,表示请求体的大小。 3. **写入文件数据*...
<form method="post" action="/upload" enctype="multipart/form-data"> <input type="file" name="file" accept="image/*,application/pdf"> 上传 ``` 这个页面包含一个文件输入字段和一个提交按钮,用户...
<form method="POST" action="/upload" enctype="multipart/form-data"> <input type="file" name="file" /> <button type="submit">Upload ``` 3. **编写Controller**: 在后端,我们需要创建一个...
`upload_file_service.zip` 提供的文件服务可能是一个用于处理客户端上传文件的Java服务端实现。本文将深入探讨Java中处理文件上传的关键知识点,包括jsp源码的使用、文件服务的实现以及相关的最佳实践。 1. **...
<form action="upload.action" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" value="Upload"/> ``` 当用户提交表单后,Struts2框架会调用Action类的...
本主题“FileUploadAndDownLoad”将深入讲解如何使用JSP(JavaServer Pages)技术来实现这一功能。JSP是一种基于Java的服务器端脚本语言,它允许开发人员在HTML页面中嵌入Java代码,从而动态生成网页内容。 首先,...
标题中的“Flex-Java-file-upload.zip_flex”表明这是一个关于使用Adobe Flex与Java进行文件上传交互的教程。Flex是一种基于ActionScript的开放源代码框架,用于构建富互联网应用程序(RIA)。它允许开发者创建动态...
<form id="fileupload" action="/Home/UploadFiles" method="POST" enctype="multipart/form-data"> <input type="file" name="files[]" multiple> 上传 ``` 这里的`<input type="file" name="files[]" multiple...
文件上传是HTTP请求的一种类型,通常通过POST请求实现,使用`multipart/form-data`编码类型来发送文件数据。 在Spring Boot应用中,我们可以使用`@Controller`注解来创建一个处理HTTP请求的控制器。然后,使用`@...
Java SSH(Struts2、Spring、Hibernate)是一个常见的企业级应用开发框架组合,用于构建高效、可维护的Web应用程序。SSH框架的结合使得开发者能够利用Struts2的MVC设计模式,Spring的依赖注入和事务管理,以及...