j2me如何上传文件

2024-04-03 01:58
文章标签 上传 j2me

本文主要是介绍j2me如何上传文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在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如何上传文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/871636

相关文章

Spring MVC 图片上传

引入需要的包 <dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-

在SSH的基础上使用jquery.uploadify.js上传文件

在SSH框架的基础上,使用jquery.uploadify.js实现文件的上传,之前搞了好几天,都上传不了, 在Action那边File接收到的总是为null, 为了这个还上网搜了好多相关的信息,但都不行,最后还是搜到一篇文章帮助到我了,希望能帮助到为之困扰的人。 jsp页面的关键代码: <link rel="stylesheet" type="text/css" href="${page

【CTF Web】BUUCTF Upload-Labs-Linux Pass-13 Writeup(文件上传+PHP+文件包含漏洞+PNG图片马)

Upload-Labs-Linux 1 点击部署靶机。 简介 upload-labs是一个使用php语言编写的,专门收集渗透测试和CTF中遇到的各种上传漏洞的靶场。旨在帮助大家对上传漏洞有一个全面的了解。目前一共20关,每一关都包含着不同上传方式。 注意 1.每一关没有固定的通关方法,大家不要自限思维! 2.本项目提供的writeup只是起一个参考作用,希望大家可以分享出自己的通关思路

Vue3上传图片报错:Current request is not a multipart request

当你看到错误 "Current request is not a multipart request" 时,这通常意味着你的服务器或后端代码期望接收一个 multipart/form-data 类型的请求,但实际上并没有收到这样的请求。在使用 <el-upload> 组件时,如果你已经设置了 http-request 属性来自定义上传行为,并且遇到了这个错误,可能是因为你在发送请求时没有正确地设置

OpenStack:Glance共享与上传、Nova操作选项解释、Cinder操作技巧

目录 Glance member task Nova lock shelve rescue Cinder manage local-attach transfer backup-export 总结 原作者:int32bit,参考内容 从2013年开始折腾OpenStack也有好几年的时间了。在使用过程中,我发现有很多很有用的操作,但是却很少被提及。这里我暂不直接

使用http-request 属性替代action绑定上传URL

在 Element UI 的 <el-upload> 组件中,如果你需要为上传的 HTTP 请求添加自定义的请求头(例如,为了通过身份验证或满足服务器端的特定要求),你不能直接在 <el-upload> 组件的属性中设置这些请求头。但是,你可以通过 http-request 属性来自定义上传的行为,包括设置请求头。 http-request 属性允许你完全控制上传的行为,包括如何构建请求、发送请

Vue3图片上传报错:Required part ‘file‘ is not present.

错误 "Required part 'file' is not present" 通常表明服务器期望在接收到的 multipart/form-data 请求中找到一个名为 file 的部分(即文件字段),但实际上没有找到。这可能是因为以下几个原因: 请求体构建不正确:在发送请求时,可能没有正确地将文件添加到 FormData 对象中,或者使用了错误的字段名。 前端代码错误:在前端代码中,可能

【SpringMVC学习06】SpringMVC中实现文件上传

1. 环境准备 springmvc上传文件的功能需要两个jar包的支持,如下 2. 单个文件的上传 2.1 前台页面 简单的写一下前台页面,注意一点的是form表单中别忘了写enctype=”multipart/form-data”属性: <tr><td>商品图片</td><td><c:if test="${itemsCustom.pic !=null}"><img src="/f

使用Vant Uploader 文件上传,后端java中MultipartFile接收不到文件问题解决

问题 在Uploader组件 after-read回调函数将获取的file对象上传到服务器。 <van-uploader:after-read="uploadFile"/>uploadFile(file) {const data = new FormData();data.

git命令上传代码到gitHub、gitLab

1 、输入git账号和密码 git config --global user.name"git账号" git config --global user.name"密码" 2.添加要上传的SSH (如果你的文件已经有了SSH,删除本身有的)git remote rm origin 添加 git remote add origin SSH或http 3 添加本地的所有文件  git ad