好久没写帖子了,写个帖子溜达溜达 先介绍一下,我这个整合的环境,已经开发环境。 (1)服务器是java语言写的,用的是jersey 1.18。现在地址是:https://jersey.java.net/download.html。主页是:https://jersey.java.net/ 这个官网里面有详细的文档说明和demo已经想过的信息,支持spring的。 下面是移动端的开发说明: (2)android端的也是使用的第三放的框架,使用的是android-async-http这个开源的库,下载地址是:https://github.com/loopj/android-async-http. 这个开发者的官网是:http://loopj.com/,下面有好多开源的代码,值得学习。 (3)IOS端的开发使用的是开发库是:AFNETworking这个优秀的开源库。下载学习地址是:https://github.com/AFNetworking/AFNetworking 文档和资料在:http://cocoadocs.org/docsets/AFNetworking/2.0.0/,在其官网有很多好的资料哦,在ios我比较喜欢这个网络开源请求库,当然都有其他的开源库了,每个人都有各自的喜好而已,哈哈 服务器的demo解释说明如下,手下是要搭建问服务,写个helloworld,jersey 我是在Tomcat下运行, 需要的jar包如下,对于android开发者来说,有些相互依赖的包,对我们理解上有一点难,至于我只能一点点根据异常寻找我项目中需要的相互依赖包。 所需要的包截图如下: 下面我将上传我所需要的jar包: lib.zip 继续的是配置我的Web.xml文件如下 其实也没怎么配置就是很简单的一个文件而已
[XML]
查看源文件
复制代码
下面将要写的是一个单独上传图片的接口:
[AppleScript]
查看源文件
复制代码
@POST @Path("uploadFile") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public Fuck uploadFile(@FormDataParam("file") InputStream file, @FormDataParam("fileName") String fileName) { // String fileFullName = fileDisposition.getFileName(); if(file==null) { Fuck fuck = new Fuck(); fuck.setAb("传入的图片为null"); fuck.setB(-1); return fuck; } try { File path = new File("D:/upload"); File filep = new File(path, fileName); if (!filep.exists()) { filep.createNewFile(); } OutputStream outputStream = new FileOutputStream(filep); // "D:\\upload", fileName // + fileFullName.substring(fileFullName.indexOf("."), // fileFullName.length()))); int length = 0; byte[] buff = new byte[256]; while (-1 != (length = file.read(buff))) { outputStream.write(buff, 0, length); } file.close(); outputStream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Fuck fuck = new Fuck(); return fuck; } 批量上图片有个问题是,不知道具体要上传几张图片的呢,所以写法和上述很多有很大差异 代码如下:
[Java]
查看源文件
复制代码
上面的核心代码就是这样的,也是服务器的代码,可以借鉴一下。 下面是android的上传代码,(一定要服务器部署好了,才能上传,别瞎整)
[Java]
查看源文件
复制代码
批量上传如下:
[Java]
查看源文件
复制代码
上面的file是因为我比较懒,写的一个集合List<File> files=new ArrayList<File>(); 下面是IOS代码的上传图片; 单张上传:
[Objective-C]
查看源文件
复制代码
批量上传如下:
[Objective-C]
查看源文件
复制代码
刚才说的是“application/octet-stream”,其本质就是一个Http content一个协议,属于Http协议,对照表如下:http://tool.oschina.net/commons |
图片上传,android ios Java服务器整合
最新推荐文章于 2021-02-28 18:18:12 发布