Java 如何将通用文件发送到jersey服务并正确接收?
我正在开发一个Jersey服务,它使用Dropbox的API 我需要将一个通用文件发布到我的服务中(该服务将能够像使用Dropbox API一样管理各种文件) 客户端 因此,我实现了一个简单的客户端:Java 如何将通用文件发送到jersey服务并正确接收?,java,rest,jersey,fileinputstream,Java,Rest,Jersey,Fileinputstream,我正在开发一个Jersey服务,它使用Dropbox的API 我需要将一个通用文件发布到我的服务中(该服务将能够像使用Dropbox API一样管理各种文件) 客户端 因此,我实现了一个简单的客户端: 打开文件 创建到URL的连接 设置正确的HTTP方法 创建FileInputStream,并使用字节缓冲区将文件写入连接的输出流 这是客户端测试代码 public class Client { public static void main(String args[]) throws I
- 打开文件
- 创建到URL的连接
- 设置正确的HTTP方法
- 创建
,并使用字节缓冲区将文件写入连接的输出流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);
}
}
}
服务器端
我有一个方法:
- 获取一个
作为参数InputStream
- 创建与原始文件具有相同名称和类型的文件
@PUT
@Path("/upload")
@Consumes("application/pdf")
public Response uploadMaterial(InputStream is) throws IOException {
String name = "info";
String type = "exerc";
String description = "not defined";
Integer c = 10;
Integer p = 131;
File f = null;
try {
f = new File("welcome.pdf");
OutputStream out = new FileOutputStream(f);
byte buf[] = new byte[1024];
int len;
while ((len = is.read(buf)) > 0)
out.write(buf, 0, len);
out.close();
is.close();
System.out.println("\nFile is created........");
} catch (IOException e) {
throw new WebApplicationException(Response.Status.BAD_REQUEST);
}
//I need to pass a java.io.file object to this method
professorManager.uploadMaterial(name, type, description, c,p, f);
return Response.ok("<result>File " + name + " was uploaded</result>").build();
}
@PUT
@路径(“/upload”)
@使用(“应用程序/pdf”)
公共响应上载材料(InputStream is)引发IOException{
String name=“info”;
字符串类型=“exerc”;
String description=“未定义”;
整数c=10;
整数p=131;
文件f=null;
试一试{
f=新文件(“welcome.pdf”);
OutputStream out=新文件OutputStream(f);
字节buf[]=新字节[1024];
内伦;
而((len=is.read(buf))>0)
out.write(buf,0,len);
out.close();
is.close();
System.out.println(“\n文件已创建…”);
}捕获(IOE异常){
抛出新的WebApplicationException(Response.Status.BAD_请求);
}
//我需要向这个方法传递一个java.io.file对象
上传材料(名称、类型、描述、c、p、f);
返回Response.ok(“已上载文件”+name+).build();
}
此实现仅适用于文本文件。如果我试图发送一个简单的PDF文件,收到的文件是不可读的(在我把它保存在磁盘上之后)
我怎样才能满足我的要求?有人能给我建议解决办法吗 您的客户端代码有错误
while ((len = is.read(buf)) > 0) {
...
is.read(buf);
...
}
在每次迭代中,您将从InputStream
读取两次。从循环的主体中删除read
语句,就可以了
您还说过,问题中提供的代码适用于文本文件。我想那也不行。从你试图上传的文件中读取两次意味着你只上传了文件内容的一半。半个文本文件仍然是文本文件,但半个PDF只是垃圾,所以你不能打开后者。您应该仔细检查上传和保存的文本文件的内容是否与原始文本文件相同。您可以发布客户端和服务器代码吗?如果没有这些,很难判断出是什么问题。谢谢你的回复。我已经添加了一些代码。我借此机会问您另一个问题:如何使用apache httpclient库实现相同的客户端功能?@pierus抱歉,但我不熟悉apache的httpclient。但是,如果您要使用Jersey,那么我建议您使用与Jersey资源交互的工具(事实上,任何其他RESTful接口)。