上传图片从BlackBerry服务器 [英] Upload image to server from Blackberry

查看:295
本文介绍了上传图片从BlackBerry服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想的图像发送到服务器。我转换的图像的字节数组。然后,我尝试了code从Java ME的 HTTP POST多部分文件上传诺基亚开发者论坛。

我也尝试从的 HTTP POST和传递的URL参数关于黑莓论坛。

我传递参数,并得到响应code 200元。但图像不会被发送到服务器。我坚持这一点。


解决方案

  {尝试FIS的FileConnection =(的FileConnection)Connector.open(文件名);
为InputStream的InputStream = fis.openInputStream();ByteArrayOutputStream BOS =新ByteArrayOutputStream();
INT缓冲区大小= 1024;
字节[]缓冲区=新的字节[缓冲区大小]
INT长度= 0;
而((长度= inputStream.read(缓冲液))!= - 1)
{
    bos.write(缓冲液,0,长度);
}
字节[]为imageData = bos.toByteArray();康涅狄格州的HttpConnection =(HttpConnection的)Connector.open(URL,Connector.READ_WRITE);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,
    HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA
                        +;边界=+边界);    conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,
                将String.valueOf(imagedata.length));
conn.setRequestProperty(X-边缘反code含量,无);ByteArrayOutputStream出=新ByteArrayOutputStream();
OutputStream的finalOut = conn.openOutputStream();字符串换行=\\ r \\ n;
out.write(newLine.getBytes());
out.write( - 的getBytes());
out.write(boundary.getBytes());
out.write(newLine.getBytes());
字符串contDisp =内容处置:表格数据;
    名字= \\文件\\;文件名= \\image.jpg的\\;
字符串contEnc =内容传输编码:二进制
字符串类型=内容类型:image / JPEG;
out.write(contDisp.getBytes());
out.write(newLine.getBytes());
out.write(type.getBytes());
out.write(newLine.getBytes());
out.write(contEnc.getBytes());
out.write(newLine.getBytes());
out.write(newLine.getBytes());
out.write(为imageData);
out.write(newLine.getBytes());
out.write( - 的getBytes());
out.write(boundary.getBytes());
out.write( - 的getBytes());
out.write(newLine.getBytes());
finalOut.write(out.toByteArray());了out.flush();
out.close();finalOut.flush();
finalOut.close();
InputStream的河道= conn.openInputStream();
INT CH = 0;
StringBuffer的buffesr =新的StringBuffer();
而((CH = instream.read())!= - 1)
{
    buffesr.append((char)的CH);
}赶上(例外五){
    // TODO:处理异常
}

I am trying to send an image to server. I converted the image to a byte array. I then tried the code from "HTTP Post multipart file upload in Java ME" on the Nokia developer forums.

I also tried the code from "HTTP POST and passing parameters in URLs" on the BlackBerry forums.

I am passing parameters and getting response code 200. But image is not sent to the server. I am stuck on this.

解决方案

try{

FileConnection fis=(FileConnection)Connector.open(filename);
InputStream inputStream = fis.openInputStream();

ByteArrayOutputStream bos=new ByteArrayOutputStream();
int buffersize=1024;
byte[] buffer=new byte[buffersize];
int length=0;
while((length=inputStream.read(buffer))!=-1)
{
    bos.write(buffer,0,length);
}
byte[] imagedata=bos.toByteArray();

HttpConnection conn = (HttpConnection) Connector.open(Url, Connector.READ_WRITE);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,                    
    HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA
                        + ";boundary=" + boundary);

    conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,
                String.valueOf(imagedata.length));
conn.setRequestProperty("x-rim-transcode-content", "none");

ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream finalOut = conn.openOutputStream();

String newLine = "\r\n";
out.write(newLine.getBytes());
out.write("--".getBytes());
out.write(boundary.getBytes());
out.write(newLine.getBytes());
String contDisp = "Content-Disposition:form-data;  
    name=\"file\";filename=\"Image.jpg\"";
String contEnc = "Content-Transfer-Encoding: binary";
String type = "Content-Type:image/jpeg";
out.write(contDisp.getBytes());
out.write(newLine.getBytes());
out.write(type.getBytes());
out.write(newLine.getBytes());
out.write(contEnc.getBytes());
out.write(newLine.getBytes());
out.write(newLine.getBytes());
out.write(imagedata);
out.write(newLine.getBytes());
out.write("--".getBytes());
out.write(boundary.getBytes());
out.write("--".getBytes());
out.write(newLine.getBytes());
finalOut.write(out.toByteArray());

out.flush();
out.close();

finalOut.flush();
finalOut.close();
InputStream instream=conn.openInputStream();
int ch=0;
StringBuffer buffesr=new StringBuffer();
while((ch=instream.read())!=-1)
{
    buffesr.append((char)ch);
}



catch (Exception e) {
    // TODO: handle exception
}

这篇关于上传图片从BlackBerry服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆