iphone客户端上传图片到服务器
本文转载至 http://blog.sina.com.cn/s/blog_4c70701801012inq.html
上传图片说明
1. 关键字说明
String pictureFileName; //上传图片名
String pictureContentType; //上传图片类型
String pictureContent; //上传图片的字符串形式
2. 上传流程
3. 代码
3.1 图片内容从字符串形式转换成二进制形式
|
public static byte[] hex2byte(String str) { // 字符串转二进制 if (str == null) return null; str = str.trim(); str = str.replace(" ", ""); int len = str.length(); if (len == 0 || len % 2 == 1) return null; byte[] b = new byte[len / 2]; try { for (int i = 0; i < str.length(); i += 2) { b[i / 2] = (byte) Integer .decode("0x" + str.substring(i, i + 2)).intValue(); } return b; } catch (Exception e) { return null; } } |
3.2 将二进制流写入文件中
|
public static File fileBuild(String pictureContent){ File file = new File(F_NAME); //存放二进制内容的指定文件 //去除前后两个尖括号 pictureContent = pictureContent.substring(1, pictureContent.length()-1); try{ if(!file.exists()){ file.createNewFile(); } // 将字符串转换成二进制,用于显示图片 int nRead = 0; byte[] imgByte = StrToByte.hex2byte(pictureContent); byte[] b = new byte[1024]; InputStream in = new ByteArrayInputStream( imgByte ); FileOutputStream output = new FileOutputStream(F_NAME,false); while( ( nRead = in.read(b) ) != -1 ){ output.write( b, 0, nRead ); } output.flush(); output.close(); in.close(); }catch(Exception e){ e.printStackTrace(); } return file; } |
3.3 将二进制流转换成图片并存放在相应位置
|
private static final int BUFFER_SIZE=16*1024; private static final String F_NAME ="D:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp1\\work\\Catalina\\localhost\\jy\\upload_7d68c7b2_13633574de8__8000_00000000.tmp"; //封装文件对象 private static void copy(File src,File dst){ InputStream in=null; OutputStream out=null; try { in=new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE); out=new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE); byte[] buffer=new byte[BUFFER_SIZE]; int len=0; while((len=in.read(buffer))>0){ out.write(buffer, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally{ if(null!=in){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if(null!=out){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static String fileexecute(int id,File picture,String savepath,String filename)throws Exception{ String dstPath=ServletActionContext.getServletContext().getRealPath(savepath)+"\\"+id; File mdFile=new File(dstPath); mdFile.mkdir(); File dstFile=new File(dstPath+"\\"+filename); copy(picture,dstFile); return savepath.substring(1)+"/"+id+"/"+filename; } |
iphone客户端上传图片到服务器的更多相关文章
- 通过android 客户端上传图片到服务器
昨天,(在我的上一篇博客中)写了通过浏览器上传图片到服务器(php),今天将这个功能付诸实践.(还完善了服务端的代码) 不试不知道,原来通过android 向服务端发送图片还真是挺麻烦的一件事. 上传 ...
- 客户端挂载NFS服务器中的共享目录(用户后台上传图片与前台上传图片放在同一个服务器上)
服务器端使用showmount命令查询NFS的共享状态 # showmount -e //默认查看自己共享的服务,前提是要DNS能解析自己,不然容易报错 # showmount -a //显示已经与客 ...
- Wcf for wp8 上传图片到服务器,将图片名字插入数据库字段(五)
环境:.NET Framework 3.5 服务: IIS EXpress托管 WCF服务程序 配置:Web.config <!--<connectionStrings> <a ...
- android 上传图片到服务器Tomcat(Struts2)
在做android开发的时候,有时你会用到图片的上传功能,在我的android项目中,我是选中图片,点击上传多张图片 android客户端上传图片部分的代码如下: package com.exampl ...
- Java乔晓松-android中上传图片到服务器Tomcat(Struts2)
在做android开发的时候,有时你会用到图片的上传功能,在我的android项目中,我是选中图片,点击上传多张图片 android客户端上传图片部分的代码如下: package com.exampl ...
- 如何搭建一个WEB服务器项目(六)—— 上传图片至服务器
上传图片(用户头像)至服务器 观前提示:本系列文章有关服务器以及后端程序这些概念,我写的全是自己的理解,并不一定正确,希望不要误人子弟.欢迎各位大佬来评论区提出问题或者是指出错误,分享宝贵经验.先谢谢 ...
- WPF上传图片到服务器文件夹
1.前端用ListBox加载显示多张图片 1 <ListBox Name="lbHeadImages" Grid.Row="1" ScrollViewer ...
- 无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同。如果服务器位于远程计算机上,请检查。。。
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET State Ser ...
- Portable Basemap Server:多数据源多客户端的底图服务器
Portable Basemap Server:多数据源多客户端的底图服务器 [poll id=”1″]2014.3.8更新v3.1~在线切片转换为MBTiles时,增加RecreateEmptyCa ...
随机推荐
- vue2计算属性computed
详见vue2.0 API<计算属性> 需求: 模板内的表达式是非常便利的,但是它们实际上只用于简单的运算.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id=&qu ...
- angular学习(二)—— Data Binding
转载请写明来源地址:http://blog.csdn.net/lastsweetop/article/details/51182106 Data Binding 在angular中.model和vie ...
- APK大小的瘦身的总结:
首先是看了博客:http://blog.csdn.net/sw950729/article/details/64919051 时.认为大神我就是马云飞写的非常有道理.全部自己就自己写了一遍.长话短说: ...
- nginx 配置优化详解
# nginx不同于apache服务器,当进行了大量优化设置后会魔术般的明显性能提升效果 # nginx在安装完成后,大部分参数就已经是最优化了,我们需要管理的东西并不多 #user nobody; ...
- 云计算之路-阿里云上-十字路口:阿里云SLB故障
2013年7月24日,18:20~18:50左右,处于阿里云云服务最前沿的SLB(负载均衡)出现故障,造成了网站不能正常访问(由于是最前沿,这次连502也看不到了). 在大家对昨日RDS故障带来的麻烦 ...
- Apache 配置:是否显示文件列表
Apache 配置:是否显示文件列表 进入虚拟主机配置文件 显示文件列表的话 options Indexes FollowSymLinks 不显示文件列表的话 options FollowSymLin ...
- dubbo笔记
使用Maven打包依赖项,启动时从本地jar中读取dubbo.xsd 最近项目用到dubbo,打包启动时报错 Failed to read schema document from http://co ...
- iOS-仿支付宝加载web网页添加进度条
代码地址如下:http://www.demodashi.com/demo/11727.html 目前市场上APP常会嵌入不少的h5页面,参照支付宝显示web页面的方式, 做了一个导航栏下的加载进度条. ...
- spring自己主动装配Bean属性
spring提供了3种类型的自己主动装配 byName:把与Bean的属性具有同样名字(或者ID)的其它Bean自己主动装配到Bean的相应属性中. byType:把与Bean的属性具有同样类型的其它 ...
- velcoity使用说明:foreach指令
http://blog.csdn.net/madding/article/details/6641020当在velocity中需要显示一个列表信息,我们会用foreach循环输出, 要求: 假如现在需 ...