使用BlackBerry上传图片 [英] Upload Image using blackberry
本文介绍了使用BlackBerry上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要上传使用MultipartPostData在BlackBerry模拟器的图像,下面是我的code,但它似乎并没有工作。我也签署了我的.cod文件。谁能帮我吗?
公共无效POSTDATA(字符串URL,字节[]数据)
{
如果(DeviceInfo.isSimulator()){
URL = URL +; deviceSide =真正的;
}
的HttpConnection httpConn = NULL;
OutputStream的OS = NULL;
InputStream为= NULL;
字符串URL =网址;
尝试{
形式的PostData =新MultipartPostData(MultipartPostData.DEFAULT_CHARSET,FALSE);
字节[] = POSTDATA数据;
form.setData(POSTDATA); httpConn =(HttpConnection的)Connector.open(URL);
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty(用户代理,黑莓);
httpConn.setRequestProperty(内容类型,多部分/表单数据);
httpConn.setRequestProperty(MIME类型,图像/ JPEG);
httpConn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,将String.valueOf(postData.length));
httpConn.setRequestProperty(内容语言,EN-US); OS = httpConn.openOutputStream();
os.write(form.getBytes()); //读取响应
StringBuffer的SB =新的StringBuffer();
是= httpConn.openDataInputStream();
INT CHR;
而((CHR = is.read())!= - 1)
sb.append((char)的CHR); 的System.out.println(结果................................+ sb.toString());
字符串结果= sb.toString();
}
赶上(例外五)
{
的System.out.println(e.toString());
}
最后{
尝试{
如果(是!= NULL)
is.close();
如果(OS!= NULL)
os.close();
如果(httpConn!= NULL)
httpConn.close();
}赶上(例外E1){
的System.out.println(e1.toString());
}
}
}
解决方案
//你必须有一个bundary格式后的数据,.cod文件必须在模拟器上工作。
httpConn =(HttpConnection的)connDesc.getConnection();
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty(用户代理,黑莓);
httpConn.setRequestProperty(内容类型,的multipart / form-data的;边界= ---------- V2ymHFg03ehbqgZCaKO6jy); OS = httpConn.openOutputStream();
//os.write(form.getBytes()); 字节[] = fileBytes {1,2,3,4}; //获取文件的字节用自己的code ByteArrayOutputStream BOS =新ByteArrayOutputStream(); bos.write(。(\\ r \\ N--+---------- V2ymHFg03ehbqgZCaKO6jy+\\ r \\ n)的getBytes());
bos.write((内容处置:表格数据;名称= \\mifoto \\;文件名= \\leo.gif \\\\ r \\ n)的getBytes());
bos.write((内容类型:image / GIF \\ r \\ n \\ r \\ n)的getBytes());
bos.write(fileBytes);
bos.write((\\ r \\ N--+---------- V2ymHFg03ehbqgZCaKO6jy+ - \\ r \\ n)的getBytes()); os.write(bos.toByteArray());
I want to upload an image in blackberry simulator using MultipartPostData, the following is my code but it does not seem to work. I have also signed my .cod file. Can anyone help me please?
public void postData(String Url, bytes[] data)
{
if (DeviceInfo.isSimulator()){
Url=Url+";deviceSide=true";
}
HttpConnection httpConn=null;
OutputStream os=null;
InputStream is=null;
String url=Url;
try {
PostData form = new MultipartPostData(MultipartPostData.DEFAULT_CHARSET, false) ;
byte [] postData = data;
form.setData(postData);
httpConn = (HttpConnection) Connector.open(url);
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("User-Agent", "BlackBerry");
httpConn.setRequestProperty("Content-Type", "multipart/form-data");
httpConn.setRequestProperty("MIME-Type", "Image/Jpeg");
httpConn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(postData.length));
httpConn.setRequestProperty("Content-Language", "en-US");
os =httpConn.openOutputStream();
os.write(form.getBytes());
//read response
StringBuffer sb = new StringBuffer();
is = httpConn.openDataInputStream();
int chr;
while ((chr = is.read()) != -1)
sb.append((char) chr);
System.out.println("Result................................ " + sb.toString());
String result=sb.toString();
}
catch(Exception e)
{
System.out.println(e.toString());
}
finally {
try{
if(is!= null)
is.close();
if(os != null)
os.close();
if(httpConn != null)
httpConn.close();
} catch(Exception e1){
System.out.println(e1.toString());
}
}
}
解决方案
//you must have a bundary format post data, the .cod file must be work on the simulator
httpConn = (HttpConnection)connDesc.getConnection();
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("user-agent", "BlackBerry");
httpConn.setRequestProperty("content-type", "multipart/form-data; boundary=----------V2ymHFg03ehbqgZCaKO6jy");
os = httpConn.openOutputStream();
//os.write(form.getBytes());
byte[] fileBytes = {1,2,3,4}; //retrieve file bytes with your own code
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(("\r\n--" + "----------V2ymHFg03ehbqgZCaKO6jy" + "\r\n").getBytes());
bos.write(("Content-Disposition: form-data; name=\"mifoto\"; filename=\"leo.gif\"\r\n").getBytes());
bos.write(("Content-Type: image/gif\r\n\r\n").getBytes());
bos.write(fileBytes);
bos.write(("\r\n--" + "----------V2ymHFg03ehbqgZCaKO6jy" + "--\r\n").getBytes());
os.write(bos.toByteArray());
这篇关于使用BlackBerry上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文