Commit ab530c7dfaf490ff388c738d290752e0a5c983af
1 parent
d3a3fa7ad4
Exists in
master
and in
6 other branches
将本地图片转换成Base64编码字符串
Showing 1 changed file with 63 additions and 3 deletions
platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/TestController.java
View file @
ab530c7
| ... | ... | @@ -51,13 +51,14 @@ | 
| 51 | 51 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | 
| 52 | 52 | import org.springframework.stereotype.Controller; | 
| 53 | 53 | import org.springframework.web.bind.annotation.*; | 
| 54 | +import sun.misc.BASE64Encoder; | |
| 54 | 55 | |
| 55 | 56 | import javax.servlet.http.HttpServletRequest; | 
| 56 | 57 | import javax.servlet.http.HttpServletResponse; | 
| 57 | 58 | import javax.validation.Valid; | 
| 58 | -import java.io.File; | |
| 59 | -import java.io.IOException; | |
| 60 | -import java.io.Serializable; | |
| 59 | +import java.io.*; | |
| 60 | +import java.net.HttpURLConnection; | |
| 61 | +import java.net.URL; | |
| 61 | 62 | import java.util.*; | 
| 62 | 63 | import java.util.regex.Pattern; | 
| 63 | 64 | |
| ... | ... | @@ -5143,6 +5144,65 @@ | 
| 5143 | 5144 | @TokenRequired | 
| 5144 | 5145 | public BaseObjectResponse syncBabyArchivesAddress(@RequestParam(required = false) String hospitalId) { | 
| 5145 | 5146 | return babySieveFacede.syncBabyArchivesAddress(hospitalId); | 
| 5147 | + } | |
| 5148 | + | |
| 5149 | + /** | |
| 5150 | + * 将本地图片转换成Base64编码字符串 | |
| 5151 | + * @return imgUrl | |
| 5152 | + */ | |
| 5153 | + @RequestMapping(method = RequestMethod.GET, value = "/convertBase64") | |
| 5154 | + @ResponseBody | |
| 5155 | + public String convertBase64(String imgUrl) { | |
| 5156 | + URL url = null; | |
| 5157 | + InputStream is = null; | |
| 5158 | + ByteArrayOutputStream outStream = null; | |
| 5159 | + HttpURLConnection httpUrl = null; | |
| 5160 | + try{ | |
| 5161 | + url = new URL(imgUrl); | |
| 5162 | + httpUrl = (HttpURLConnection) url.openConnection(); | |
| 5163 | + httpUrl.connect(); | |
| 5164 | + httpUrl.getInputStream(); | |
| 5165 | + is = httpUrl.getInputStream(); | |
| 5166 | + | |
| 5167 | + outStream = new ByteArrayOutputStream(); | |
| 5168 | + //创建一个Buffer字符串 | |
| 5169 | + byte[] buffer = new byte[1024]; | |
| 5170 | + //每次读取的字符串长度,如果为-1,代表全部读取完毕 | |
| 5171 | + int len = 0; | |
| 5172 | + //使用一个输入流从buffer里把数据读取出来 | |
| 5173 | + while( (len=is.read(buffer)) != -1 ){ | |
| 5174 | + //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 | |
| 5175 | + outStream.write(buffer, 0, len); | |
| 5176 | + } | |
| 5177 | + // 对字节数组Base64编码 | |
| 5178 | + return new BASE64Encoder().encode(outStream.toByteArray()); | |
| 5179 | + }catch (Exception e) { | |
| 5180 | + e.printStackTrace(); | |
| 5181 | + } | |
| 5182 | + finally{ | |
| 5183 | + if(is != null) | |
| 5184 | + { | |
| 5185 | + try { | |
| 5186 | + is.close(); | |
| 5187 | + } catch (IOException e) { | |
| 5188 | + e.printStackTrace(); | |
| 5189 | + } | |
| 5190 | + } | |
| 5191 | + if(outStream != null) | |
| 5192 | + { | |
| 5193 | + try { | |
| 5194 | + outStream.close(); | |
| 5195 | + } catch (IOException e) { | |
| 5196 | + e.printStackTrace(); | |
| 5197 | + } | |
| 5198 | + } | |
| 5199 | + if(httpUrl != null) | |
| 5200 | + { | |
| 5201 | + httpUrl.disconnect(); | |
| 5202 | + } | |
| 5203 | + } | |
| 5204 | + return imgUrl; | |
| 5205 | + | |
| 5146 | 5206 | } | 
| 5147 | 5207 | } |