Servlet利用base64和json向客户端传输图片
2017-05-15 20:49
495 查看
更新:这种方法出毛病了!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 4000 (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(); }
相关文章推荐
- Android客户端和Servlet服务端的JSON传输(注释详细到啰嗦的地步,欢迎新手学习)
- 客户端和应用服务器之间利用HttpClient和Servlet交互JSON对象进行通信
- 服务端和Android客户端利用Socket传输JSON数据
- 使用volley实现简单的操作,json传输,网络图片显示
- js压缩上传的图片,并转化为base64字符串传输
- 【Servlet】利用Servlet3.0标准与JSTL表达式实现文件上传系统,支持图片上传后显示
- 笔记:使用json传输图片,根据个人经验:我做不到,想了个办法将文件转变成字符串并压缩
- Servlet+spring+mybatis结合传输json数组
- Android利用Volley异步加载数据(JSON和图片)完整示例
- js上传图片用ajax传输base64
- 利用FileReader实现客户端预览图片
- 利用PHP将图片转换成base64编码的实现方法
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)
- thrift编写服务器/客户端实现图片传输(c++)
- android客户端和java服务端之间用socket来传输图片
- Android利用Volley异步加载数据(JSON和图片)完整示例
- 利用Data URL(data:image/jpg;base64,)将小图片生成数据流形式
- 利用PHP将图片转换成base64编码的实现方法
- Android客户端与服务器端通过SOCKET连接进行读写(将JSON数据转化为字符串后传输)
- android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)