因为工作原因,很久时间木有更新了,不怕有时间久继续更新:
基本的resteasy的搭建详情请看第一、二波,我这里指的上传是指利用resteasy来获取:客户端HTTP开发包发送的body请求-----刚开始也没撒头绪,第一时间去google,发现有哥们问同样的问题,但是却没有解决方法,于是乎就只有硬着头皮看英文文档,发现如下文档:
server端resource类代码如下:
@PUT
@Path(value = "/{folder}/{fileName}")
public Response uploadFile(@PathParam("folder") String folder,@PathParam("fileName") String fileName, @Context HttpServletRequest request)
{
File f = new File("x:\\"+fileName);
InputStream is = null;
try {
is = request.getInputStream();
FileUtils.copyInputStreamToFile(is, f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Response.ok().build();
}
请主要看下以上代码红色部分,就相当于把HttpServletRequest请求注入到了request变量中,那么通过request对象获取获取输入流,则得到了body主体部分的数据。
客户端的代码如下
String urlResouce = "http://localhost:8080/cloud-storeage-boss/muluSDF/xyz.png"; // create URL
File localFile = new File("C:\\Users\\maomao\\Pictures\\2.png");
try{
HttpURLConnection urlConnection =
(HttpURLConnection) (new URL(urlResouce)).openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("PUT");
OutputStream urlOutputStream = urlConnection.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(localFile);
IOUtils.copy(fileInputStream, urlOutputStream);
fileInputStream.close();
urlOutputStream.close();
System.out.println(urlConnection.getResponseCode());
}
catch(Exception e)
{
e.printStackTrace();
}
通过此客户端的调用,就可以将本地的2.png文件上传到服务器,并被保存为xyz.png.
上传就是如此的简单,如果大家有兴趣,下一波会讲讲form方式的提交上传或者下载文件