在项目开发的过程中,我们经常需要用到图片上传操作,传统的做法是我们将其上传到项目的所在目录中,比如说项目的target目录中,但是由于项目在重启的过程中,target文件夹下的内容会被全部清空,这意味着如果采用这种方式,那么在测试环境与开发环境;在每一次的项目重启的过程中,我们经常会遭遇图片丢失的情况,这种情况有时往往很让人头疼,因而有一个图片服务器对于项目的开发和测试而言,这是十分必要的。
在采用图片服务器的情况下,我们只需要像往常一样,由前台界面将图片上传到本地项目服务器中,然后本地的项目服务器会将该图片的数据流写入到远程的图片服务器中,由远程的图片服务器负责图片的存储,最后,远程的图片服务器会返回一个存储图片路径,在本地的服务器只需要存储该路径,在页面展示的时候,只需要在前台将该图片路径展示出来,即可完成对于远程图片服务器的调用。
FileUpLoadNew
package net.sahv.bdyz.util; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Iterator; import java.util.Map; public class FileUpLoadNew { /** * @param urlStr * @param textMap * @param fileMap * @return */ public static String formUpload(String urlStr, Map<String, String> textMap, Map<String, InputStream> fileMap) { String res = ""; HttpURLConnection conn = null; String BOUNDARY = "---------------------------123821742118716"; //boundary就是request头和上传文件内容的分隔符 try { URL url = new URL(urlStr); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(30000); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); OutputStream out = new DataOutputStream(conn.getOutputStream()); // textMap:如果传送的是文本内容 if (textMap != null) { StringBuffer strBuf = new StringBuffer(); Iterator<Map.Entry<String, String>> iter = textMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, String> entry = iter.next(); String inputName = (String) entry.getKey(); String inputValue = (String) entry.getValue(); if (inputValue == null) { continue; } strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n"); strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n"); strBuf.append(inputValue); } out.write(strBuf.toString().getBytes()); } //fileMap:如果传送的是文件流 if (fileMap != null) { Iterator<Map.Entry<String, InputStream>> iter = fileMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, InputStream> entry = iter.next(); String inputName = (String) entry.getKey(); FileInputStream inputValue = (FileInputStream) entry.getValue(); if (inputValue == null) { continue; } String contentType = "image/png"; StringBuffer strBuf = new StringBuffer(); strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n"); strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + inputName + "\"\r\n"); strBuf.append("Content-Type:" + contentType + "\r\n\r\n"); out.write(strBuf.toString().getBytes()); DataInputStream in = new DataInputStream(inputValue); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } in.close(); } } byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes(); out.write(endData); out.flush(); out.close(); // 读取返回数据 StringBuffer strBuf = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { strBuf.append(line).append("\n"); } res = strBuf.toString(); reader.close(); reader = null; } catch (Exception e) { System.out.println("发送POST请求出错。" + urlStr); e.printStackTrace(); } finally { //最后关闭链接 if (conn != null) { conn.disconnect(); conn = null; } } return res; } }比如说本地一次上传多张图片,如下图所示:
当我们点击上传后,页面显示结果:
本地图片服务器后台打印结果:
2018\04\09\1523257444813.jpg 2018\04\09\1523257444944.jpg 2018\04\09\1523257445078.jpg 2018\04\09\1523257445202.jpg 2018\04\09\1523257445360.jpg 2018\04\09\1523257445484.jpg 2018\04\09\1523257445593.jpg 2018\04\09\1523257445742.jpg图片的访问路径 = 服务器地址 + 回传的图片相对地址
即下面的地址:
http://localhost:9080/no-js/upload/2018/04/09/1523257444813.jpg其显示的结果为: