-1
嗨下面是簡單的代碼上傳文件到服務器其工作正常...但是當我通過黑莓發送圖像到服務器它做了一些工作,但沒有圖像文件在服務器請指導我在那裏會犯錯......上傳圖像到服務器使用黑莓http連接
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="img.php">
<input type="file" name="image"/>
<input type="submit" name="upload"/>
</form>
</body>
</html>
這裏是BB的Java代碼
public HttpMultipartRequest(String url, Hashtable params, Hashtable headers, 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()
{
HttpConnection hc = null;
InputStream is = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
String login = test:test;
byte[] res = null;
boolean keepGoing = true;
boolean needAuth = false;
try {
byte[] encoded = Base64OutputStream.encode(login.getBytes(), 0, login.length(), false, false);
Logger.log("Trying to connect to "+url);
hc = (HttpConnection) Connector.open(url);
while (keepGoing) {
// headers needed specifically for upload
hc.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + getBoundaryString());
hc.setRequestProperty(
HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer
.toString(postBytes.length));
hc.setRequestProperty("x-rim-transcode-content", "none");
hc.setRequestProperty("Authorization", "Basic " + new String(encoded));
hc.setRequestMethod(HttpConnection.POST);
OutputStream dout = hc.openOutputStream();
dout.write(postBytes);
dout.close();
int responseCode = hc.getResponseCode();
if (responseCode == HttpConnection.HTTP_UNAUTHORIZED) {
hc.close();
hc = (HttpConnection) Connector.open(url);
needAuth = true;
throw new Exception("Exception when uploading. RC: "+String.valueOf(responseCode)+", Unauthorized.");
} else if (responseCode == 422) {
throw new Exception("Exception when uploading. RC: "+String.valueOf(responseCode)+".");
} else if (responseCode == HttpConnection.HTTP_OK) {
int ch;
bos.reset();
is = hc.openInputStream();
while ((ch = is.read()) != -1) {
bos.write(ch);
}
res = bos.toByteArray();
keepGoing = false;
} else {
int ch;
bos.reset();
is = hc.openInputStream();
while ((ch = is.read()) != -1) {
bos.write(ch);
}
res = bos.toByteArray();
keepGoing = false;
throw new Exception("Exception when uploading. RC: "+String.valueOf(responseCode)+", "+new String(res));
}
}
hey..few天前我也面臨同樣的問題,我通過以下鏈接解決問題:https://github.com/cleverua/blackberry_multipart_post,也許這也會對你有所幫助 –
謝謝@Anzy_現在其真正的作品:) – AndroidNijx
@omer younus你可以發佈完整的解決方案,包括函數HttpMultipartRequest的調用爲例。我也需要幫助。你的解決方案將幫助我很多。謝謝。 – ejobity