使用dropbox java api上载二进制文件
你能给我指出一个代码或url,在那里我可以找到一些例子,如何使用dropbox java api和上传二进制文件,如.doc文件jpg和视频文件 目前网络上的例子只指向上传文本文件。但当我尝试使用java InputStream读取文件并将其转换为字节数组并传递到dropbox文件上载函数时,文件就会损坏。下载文件也有同样的问题。提前谢谢 问候,, 瓦鲁纳 编辑-- 代码示例使用dropbox java api上载二进制文件,java,file-upload,dropbox,Java,File Upload,Dropbox,你能给我指出一个代码或url,在那里我可以找到一些例子,如何使用dropbox java api和上传二进制文件,如.doc文件jpg和视频文件 目前网络上的例子只指向上传文本文件。但当我尝试使用java InputStream读取文件并将其转换为字节数组并传递到dropbox文件上载函数时,文件就会损坏。下载文件也有同样的问题。提前谢谢 问候,, 瓦鲁纳 编辑-- 代码示例 FileInputStream fis = new FileInputStream(file); ByteArrayOu
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte [] buf = new byte[1024];
for(int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
System.out.println("read "+ readNum + "bytes,");
}
ByteArrayInputStream inputStream2 = new ByteArrayInputStream(bos.toByteArray());
Entry newEntry = mDBApi.putFile("/uploads/"+file.getName(), inputStream2, file.toString().length(), null, null);
System.out.println("Done. \nRevision of file: " + newEntry.rev + " " + newEntry.mimeType);
return newEntry.rev;
DropboxAPI.putFile()
的第三个参数应该是从输入流读取的字节数-您正在传递文件名的长度
而不是
Entry newEntry = mDBApi.putFile("/uploads/"+file.getName(), inputStream2,
file.toString().length(), null, null);
使用
DropboxAPI.putFile()
的第三个参数应该是从输入流读取的字节数-您正在传递文件名的长度
而不是
Entry newEntry = mDBApi.putFile("/uploads/"+file.getName(), inputStream2,
file.toString().length(), null, null);
使用
我认为您不需要转换为字节数组,只需使用FileInputStream就足够了,它可以用于文件、txt以及二进制文件。下面的代码可以工作,我刚刚用JPG进行了测试
DropboxAPI<?> client = new DropboxAPI<WebAuthSession>(session);
FileInputStream inputStream = null;
try {
File file = new File("some_pic.jpg");
inputStream = new FileInputStream(file);
DropboxAPI.Entry newEntry = client.putFile("/testing.jpg", inputStream,
file.length(), null, null);
System.out.println("The uploaded file's rev is: " + newEntry.rev);
} catch (DropboxUnlinkedException e) {
// User has unlinked, ask them to link again here.
System.out.println("User has unlinked.");
} catch (DropboxException e) {
System.out.println("Something went wrong while uploading.");
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {}
}
}
DropboxAPI客户端=新的DropboxAPI(会话);
FileInputStream inputStream=null;
试一试{
File File=新文件(“some_pic.jpg”);
inputStream=新文件inputStream(文件);
DropboxAPI.Entry newEntry=client.putFile(“/testing.jpg”),inputStream,
file.length(),null,null);
System.out.println(“上传文件的版本为:“+newEntry.rev”);
}捕获(DropboxUnlinkedException){
//用户已取消链接,请让他们在此处再次链接。
System.out.println(“用户已解除链接”);
}捕获(DropBoxexException e){
System.out.println(“上传时出错”);
}catch(filenotfounde异常){
System.out.println(“未找到文件”);
}最后{
如果(inputStream!=null){
试一试{
inputStream.close();
}捕获(IOE){}
}
}
我认为您不需要转换为字节数组,只需使用FileInputStream就可以创建文件、txt和二进制文件。下面的代码可以工作,我刚刚用JPG进行了测试
DropboxAPI<?> client = new DropboxAPI<WebAuthSession>(session);
FileInputStream inputStream = null;
try {
File file = new File("some_pic.jpg");
inputStream = new FileInputStream(file);
DropboxAPI.Entry newEntry = client.putFile("/testing.jpg", inputStream,
file.length(), null, null);
System.out.println("The uploaded file's rev is: " + newEntry.rev);
} catch (DropboxUnlinkedException e) {
// User has unlinked, ask them to link again here.
System.out.println("User has unlinked.");
} catch (DropboxException e) {
System.out.println("Something went wrong while uploading.");
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {}
}
}
DropboxAPI客户端=新的DropboxAPI(会话);
FileInputStream inputStream=null;
试一试{
File File=新文件(“some_pic.jpg”);
inputStream=新文件inputStream(文件);
DropboxAPI.Entry newEntry=client.putFile(“/testing.jpg”),inputStream,
file.length(),null,null);
System.out.println(“上传文件的版本为:“+newEntry.rev”);
}捕获(DropboxUnlinkedException){
//用户已取消链接,请让他们在此处再次链接。
System.out.println(“用户已解除链接”);
}捕获(DropBoxexException e){
System.out.println(“上传时出错”);
}catch(filenotfounde异常){
System.out.println(“未找到文件”);
}最后{
如果(inputStream!=null){
试一试{
inputStream.close();
}捕获(IOE){}
}
}
发布您的代码,这样我们就可以建议您错误的地方!你好,谢谢,我用我的代码更新了这个问题。发布你的代码,这样我们就可以告诉你哪里错了!嗨,谢谢,我用我的代码更新了这个问题。谢谢你的回答,这个工作很完美。由于这个小错误,我损失了很多开发时间。谢谢你的回答,这个工作非常完美。由于这个小错误,我损失了很多开发时间。