Servlet利用base64和json向客户端传输图片
来源:互联网 发布:blu ray mac 2.16破解 编辑:程序博客网 时间:2024/05/17 05:17
更新:这种方法出毛病了!jvm堆溢出了!
原因:byte[] bytes = new byte[fileForInput.available()]这句话在遇到比较大的图片时,开的内存就大,压根就不能这么写!
解决方法一:http://stackoverflow.com/questions/9579874/out-of-memory-when-encoding-file-to-base64
解决方法二:不要传图片了!直接传URL吧!!
—————————————————————————————
其实跟上一篇blog差不多,客户端还是用HttpURLConnection。
服务器端Servlet在传输图片时,先将图片编码为base64格式(也就是一个字符串),然后放在JSONObject里,顺带着其他数据一起传给客户端。
客户端在收到JSONObject后,将base64格式字符串取出来,再解码写入到一个文件里,得到图片。
OK,下面上代码,服务器端:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub JSONObject json = new JSONObject(); //图片路径 String picPath = getServletContext().getRealPath("")+"/images/XXX.jpg"; String content = ProcessClientData.getPicBASE64(picPath); json.put("content",content); json.put("name", "XXX.jpg"); sendJson(json, response); } //base64编码函数 public String getPicBASE64(String picPath) { String content = ""; try { FileInputStream fileForInput = new FileInputStream(picPath); byte[] bytes = new byte[fileForInput.available()]; fileForInput.read(bytes); content = new sun.misc.BASE64Encoder().encode(bytes); fileForInput.close(); } catch (Exception e) { e.printStackTrace(); } return content; } //发送json函数 public void sendJson(JSONObject json,HttpServletResponse response){ try{ response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=UTF-8"); PrintWriter out = response.getWriter(); out.write(json.toString()); out.flush(); out.close(); }catch(Exception e){ } }
客户端:
JSONObject jsonRec = new JSONObject(); try { JSONObject obj = new JSONObject(); //要向服务器发的json obj.put("xxx", "XXX"); // 创建url资源 URL url = new URL("http://localhost/Test/XXXServlet"); // 建立http连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置允许输出 conn.setDoOutput(true); conn.setDoInput(true); // 设置不用缓存 conn.setUseCaches(false); // 设置传递方式 conn.setRequestMethod("POST"); // 设置维持长连接 conn.setRequestProperty("Connection", "Keep-Alive"); // 设置文件字符集: conn.setRequestProperty("Charset", "UTF-8"); // 设置文件类型: conn.setRequestProperty("contentType", "application/json"); // 开始连接请求 conn.connect(); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); // 写入请求的字符串 writer.write(obj.toString()); writer.flush(); writer.close(); //接收服务器返回的json BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")) ; String line; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line); } br.close(); conn.disconnect(); jsonRec = JSONObject.fromObject(sb.toString()); } catch (Exception e) { e.printStackTrace(); } //处理json String content = jsonRec.getString("content"); String name = jsonRec.getString("name"); BASE64Decoder decoder = new BASE64Decoder(); try { // Base64解码 byte[] bytes = decoder.decodeBuffer(content); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } // 生成图片 OutputStream outs = new FileOutputStream("C:/users/XXX/Desktop/"+name); outs.write(bytes); outs.flush(); outs.close(); } catch (Exception e) { e.printStackTrace(); }
阅读全文
0 0
- Servlet利用base64和json向客户端传输图片
- 服务端和Android客户端利用Socket传输JSON数据
- servlet向客户端返回JSON数据
- servlet 向客户端返回图片数据
- java base64 传输图片
- java base64 传输图片
- go base64图片传输
- 使用HttpURLConnection和Servlet传输json
- 客户端和应用服务器之间利用HttpClient和Servlet交互JSON对象进行通信
- Android客户端和Servlet服务端的JSON传输(注释详细到啰嗦的地步,欢迎新手学习)
- 使用base64实现socket.io传输图片和声音文件
- 使用img src Base64 实现图片的传输和预览
- 关于base64的理解(图片和秘钥传输)
- webservice 图片传输 客户端
- 图片Base64加密实现图片传输
- json传输图片
- C++利用socket的客户端和服务器之间传输文件
- (java)利用BASE64编码和解码图片文件
- Java比较器Comparable接口
- 2017江苏省赛总结
- SPPnet——空间金字塔池化
- 2017华为研发工程师编程题之明明的随机数
- 读取文本文件中数据到vector中,以每一行作为独立元素和以每一词作为独立元素
- Servlet利用base64和json向客户端传输图片
- 《北京法源寺》——群体
- 自建windows错误处理系统
- HDU1052 Tian Ji -- The Horse Racing 田忌赛马 贪心
- 微软新论文:如何利用深度特征流提高视频识别准确率(HR)
- 分数比较
- NBUT-[1674] math happy
- 使用ghost 快速安装操作系统
- 面向对象—Object