j2me如何上传文件

转载 2011年01月11日 08:36:00

在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下的读取文件、Base64编码、URLEncoder上传文件

有时需要把文件进行Base64编码成字符串来传递给服务器, 按以下顺序: 1.读取文件成byte[] 2.进行Base64编码成String 2.把得到的String进行URLEncoder ...

关于j2me对txt文件操作基础操作

最近一直在搞PDA基于MTK平台开发,对于文本文件的操作是大伤脑筋,因为j2me不能针对文本文件写操作,所以引入jsr75,但是现有的功能操作也是过于简陋,对于操作几万条的数据无论是硬件还是软件都是一...

j2me文件操作

j2me文件操作 (1)判断 手机是否支持文件系统 /** * 检测手机是否支持文件系统 * @return */ public boolean isSupportFileSystem...

j2me文件操作程序

  • 2008-01-11 17:53
  • 382KB
  • 下载

J2ME实现精确秒表计时

import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Font; import javax.microedit...

Eclipse J2ME 开发环境文件

  • 2012-03-20 10:20
  • 3.62MB
  • 下载

j2me,Http,l联网

1) .由于无线设备所能支持的网络协议非常有限,仅限于HTTP,Socket,UDP等几种协议,不同的厂家可能还支持其他网络协议,但是,MIDP 1.0规范规定,HTTP协议是必须实现的协议,而其他协...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)