| | |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import sun.misc.BASE64Encoder; |
| | | |
| | | import java.io.*; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.util.Objects; |
| | | import java.util.UUID; |
| | | |
| | |
| | | }else { |
| | | lastname=".pdf"; |
| | | } |
| | | String filePath = "D:\\project\\file\\"; |
| | | String filePath = "D:\\FFproject\\file\\"; |
| | | if(fileString == null){ |
| | | return null; |
| | | }else { |
| | |
| | | } |
| | | } |
| | | |
| | | public static String netSourceToBase64(String srcUrl,String requestMethod) { |
| | | ByteArrayOutputStream outPut = new ByteArrayOutputStream(); |
| | | byte[] data = new byte[1024 * 8]; |
| | | try { |
| | | // 创建URL |
| | | URL url = new URL(srcUrl); |
| | | // 创建链接 |
| | | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| | | conn.setRequestMethod(requestMethod); |
| | | conn.setConnectTimeout(10 * 1000); |
| | | |
| | | if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { |
| | | //连接失败/链接失效/文件不存在 |
| | | return null; |
| | | } |
| | | InputStream inStream = conn.getInputStream(); |
| | | int len = -1; |
| | | while (-1 != (len = inStream.read(data))) { |
| | | outPut.write(data, 0, len); |
| | | } |
| | | inStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | // 对字节数组Base64编码 |
| | | BASE64Encoder encoder = new BASE64Encoder(); |
| | | return encoder.encode(outPut.toByteArray()); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String get = netSourceToBase64("http://192.168.0.121:9999/admin/sys-file/platform/20230704154745.png", "GET"); |
| | | System.out.println(get); |
| | | } |
| | | |
| | | } |