我正在开发一个使用Dropbox API的Jersey服务.
我需要将一个通用文件发布到我的服务中(该服务可以管理各种文件,也可以使用Dropbox API).
客户端
所以,我实现了一个简单的客户端:
FileInputStream并使用字节缓冲区在连接的输出流上写入文件.这是客户端测试代码.
public class Client {
public static void main(String args[]) throws IOException, InterruptedException {
String target = "http://localhost:8080/DCService/REST/professor/upload";
URL putUrl = new URL(target);
HttpURLConnection connection = (HttpURLConnection) putUrl.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("content-Type", "application/pdf");
OutputStream os = connection.getOutputStream();
InputStream is = new FileInputStream("welcome.pdf");
byte buf[] = new byte[1024];
int len;
int lung = 0;
while ((len = is.read(buf)) > 0) {
System.out.print(len);
lung += len;
is.read(buf);
os.write(buf, 0, len); …Run Code Online (Sandbox Code Playgroud)