gpt4 book ai didi

java - 在 blackberry rim 应用程序中上传文件(超过 2 MB)时连接关闭

转载 作者:行者123 更新时间:2023-12-02 05:47:14 25 4
gpt4 key购买 nike

我正在尝试将文件从黑莓应用程序上传到 Amazon s3。

这是我的代码

  private synchronized void uploadFileToAmazon(CreateFileIdBean createFileIdBean)       throws UnsupportedEncodingException, IOException,ConnectionNotFoundException, ConnectionClosedException, BackupCancelledException, InterruptedException, BackupInterruptedException {

String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy";

String Policy = "{\"expiration\": \"2020-12-01T12:00:00.000Z\","
+ "\"conditions\": ["
+ "{\"bucket\": \"" + BeanFactory.getUserCreateBackupidBean().getBucketName() + "\"},"
+ "{\"x-amz-security-token\": \"" + BeanFactory.getUserCreateBackupidBean().getAmazonToken() + "\"},"
+ "{\"success_action_status\": \"201\"},"
+ "[\"starts-with\", \"$Content-Type\", \"\"],"
+ "[\"starts-with\", \"$key\", \"" + BeanFactory.getUserCreateBackupidBean().getBackupPath() + "\"]"
+ "]"
+ "}";
String encodePolicy = Base64.encode(Policy.getBytes());
String signature = uploadSignature(Policy, BeanFactory.getUserCreateBackupidBean().getAmazonSecret());
Hashtable params = new Hashtable();
params.put("key", BeanFactory.getUserCreateBackupidBean().getBackupPath() + "/" + BeanFactory.getUserCreateFileIdBean().getFileId());
params.put("AWSAccessKeyId", BeanFactory.getUserCreateBackupidBean().getAmazonKey());
params.put("Content-Type", createFileIdBean.getFileTypeContent());
params.put("x-amz-security-token", BeanFactory.getUserCreateBackupidBean().getAmazonToken());
params.put("policy", encodePolicy);
params.put("success_action_status", "201");
params.put("Signature", signature);

send(BOUNDARY, "http://" + BeanFactory.getUserCreateBackupidBean().getBucketName() + ".s3.amazonaws.com/", params, "file", BeanFactory.getUserCreateFileIdBean().getFileId(), createFileIdBean.getFileTypeContent(), createFileIdBean.getFileByte(), createFileIdBean);

}

private synchronized String getBoundaryMessage(String boundary, Hashtable params, String fileField, String fileName, String fileType, byte[] fileBytes, CreateFileIdBean createFileIdBean) {
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");
}

return res.toString();
}

private synchronized void send(String boundarry, String url, Hashtable params, String fileField, String fileName, String fileType, byte[] fileBytes, CreateFileIdBean createFileIdBean) throws IOException,ConnectionClosedException,ConnectionNotFoundException, BackupCancelledException, InterruptedException, BackupInterruptedException {


StringBuffer buffer = new StringBuffer();

HttpConnection hc = null;
InputStream is = null;
InputStream inputFileDataStream = null;
DataOutputStream dout = null;

String boundary = boundarry;
StringBuffer res = new StringBuffer();

int ch;

String boundaryMessage = getBoundaryMessage(boundary, params, fileField, fileName, fileType, fileBytes, createFileIdBean);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(boundaryMessage.getBytes());

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");

bos.write(res.toString().getBytes());

String end = "\r\n"+"--"+boundary+"\r\n"+"Content-Disposition: form-data; name=\""+"submit"+"\"\r\n"+"\r\n"+"Upload to Amazon S3"+"\r\n"+"--"+boundary+"--\r\n";

try {

hc = (HttpConnection) Connector.open(url+Resources.getConnectionString(), Connector.READ_WRITE,true);
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundarry);
hc.setRequestProperty("User-Agent", Resources.getUserAgentString());
//hc.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
hc.setRequestProperty("Content-Language", "en-US");
hc.setRequestProperty("Connection", "Keep-Alive");
hc.setRequestProperty("Keep-Alive", "300");
hc.setRequestProperty("Expect", "100-continue");

hc.setRequestProperty("Content-Length", (bos.toByteArray().length+createFileIdBean.getFileSize()+end.getBytes().length)+"");
hc.setRequestProperty("Content-length", (bos.toByteArray().length+createFileIdBean.getFileSize()+end.getBytes().length)+"");

dout = new DataOutputStream(hc.openDataOutputStream());

dout.write(bos.toByteArray());


inputFileDataStream = readInputStream(createFileIdBean.getFilePath());

while ((ch = inputFileDataStream.read()) != -1) {

dout.write(ch);

}

dout.write(end.getBytes());

dout.flush();
//dout.close();


is = hc.openDataInputStream();

BeanFactory.getUserUploadFileBean().setResponseCode(hc.getResponseCode() + "");
BeanFactory.getUserUploadFileBean().setResponseMessage(hc.getResponseMessage());

while ((ch = is.read()) != -1) {

buffer.append((char) ch);
}

System.out.println("buffer"+buffer);

}
catch (IOException e) {
throw new BackupInterruptedException(Constants.ERROR_IN_UPLOAD);

} finally {
try {
if (is != null) {
is.close();
}
if (hc != null) {
hc.close();
}
if(inputFileDataStream !=null)
{
inputFileDataStream.close();
}
if(dout !=null)
{
dout.close();
}


} catch (IOException e2) {
System.out.println("aa"+e2.getMessage());
throw e2;
}
}
}

private synchronized String uploadSignature(String policy, String secretKey) throws UnsupportedEncodingException {

String encodePolciy = Base64.encode(policy.getBytes());
HMac m = new HMac(new SHA1Digest());
m.init(new KeyParameter(secretKey.getBytes("UTF-8")));
byte[] bytes = encodePolciy.getBytes("UTF-8");
m.update(bytes, 0, bytes.length);
byte[] mac = new byte[m.getMacSize()];
m.doFinal(mac, 0);
String signature = Base64.encode(mac);
return signature;
}

private synchronized InputStream readInputStream(String path) throws IOException {

FileConnection fc = null;
InputStream is = null;

fc = (FileConnection) Connector.open(path.toString(), Connector.READ);
if (!fc.exists()) {
Settings.ERROR_MESSAGE = "File doesn't exist!";
//throw new BackupInterruptedException(Settings.ERROR_MESSAGE);
} else {
is = fc.openInputStream();
}
if(fc !=null)
{
fc.close();
}
return is;
}

当我尝试在上传数据后获取响应代码(在 BeanFactory.getUserUploadFileBean().setResponseCode(hc.getResponseCode() + ""); 行中)时,它总是出现连接关闭异常。对于小于 2MB 的文件,其工作正常。请帮助我...

最佳答案

我对 HttpConnection 的体验是,它总是在发送任何内容之前缓冲完整的有效负载。对于 BlackBerry Curve 8520,16mb 的数据最终会导致设备无法使用。我通过使用原始 SocketConnection 来解决这个问题,并直接在我的应用程序中编写 HTTP 部分,这样我就可以确保在字节到达套接字之前没有任何过多的缓冲。

我后来意识到可能有用的一个领域是使用 HTTP-Chunked模式与内置 HttpConnection 对象。由于 HTTP-Chunked 从根本上来说是一种流机制,因此它可以让您摆脱默认情况下获得的“全部缓冲”逻辑。

关于java - 在 blackberry rim 应用程序中上传文件(超过 2 MB)时连接关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23944875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com