j2me如何上传文件
来源:互联网 发布:网络机顶盒软件app 编辑:程序博客网 时间:2024/05/21 06:18
在nokia的论坛里看到,感觉很详细,在此分享。
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";
}
?>
- j2me如何上传文件
- j2me上传文件的问题
- 【J2ME】J2ME下的读取文件、Base64编码、URLEncoder上传文件
- 如何实现文件上传
- 如何上传文件
- struts2 如何 上传文件
- 如何实现文件上传
- Github如何上传文件
- laravel如何上传文件
- java如何上传文件
- UE4如何上传文件
- J2ME 文件上传 实例(FileConnector链接文件系统,非RMS)
- Ajax上传如何实现文件上传进度条
- j2me上传积分
- j2me中文件上传
- HTML教程 - 如何上传文件
- 如何利用PHP上传文件
- php如何实现文件上传 ?
- JDBC连接各种数据库的方法
- Linux 中内核与应用程序的通信手段之一——套接字
- DataBase和DataSet同步数据
- Ubuntu下串口工具
- java环境下读取xml文件的方法
- j2me如何上传文件
- 赋值表达式——Ada应用实例之十一
- 转载的js小例子。。。
- xml bianji
- C 标准 头文件 总结
- 年终盘点:2010年半导体产业的十大进展-转自老杳
- 我想,在这一年里,现在应该是个开始(昨天想好的,今天写的)
- 诺基亚N900安装Android 2.2改版系统Nitdroid教程
- iframe 自适应高度总结