基于Base64的图片转字符串-java和C#互通问题

前一段时间做了一个小项目,把一张图片传到服务器端,功能很简单,服务器端用.net MVC4实现的,客户端是winform程序。

昨天领导说客户端要移植一班Android程序,就花了点时间做了一个,传的时候发现java转出来的字符串跟C#的不一样,还原不了。

因为Base64是一个统一的标准,所以按道理应该是一样的才对。

期初用的是sun.misc.BASE64Encoder类,之后换了java.util.Base64.Encoder类就一致了。

问题解决,贴一下代码

C#:

 1  public static string File2String(string path)
 2         {
 3             if (string.IsNullOrEmpty(path))
 4             {
 5                 throw new Exception("文件路径不能为空!");
 6             }
 7             try
 8             {
 9                 using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
10                 {
11                     byte[] buffByte = new byte[fs.Length];
12                     fs.Read(buffByte, 0, (int)fs.Length);
13                     string str = Convert.ToBase64String(buffByte);
14                     return str;
15                 }
16             }
17             catch (DirectoryNotFoundException ex)
18             {
19                 throw ex;
20             }
21             catch (Exception ex)
22             {
23                 throw ex;
24             }
25         }
26 
27         public static bool String2file(string inputString, string path, string filename)
28         {
29             if (string.IsNullOrEmpty(inputString))
30             {
31                 throw new Exception("输入数据不能为空");
32             }
33             if (string.IsNullOrEmpty(path))
34             {
35                 throw new Exception("路径不能为空!");
36             }
37             if (string.IsNullOrEmpty(filename))
38             {
39                 throw new Exception("文件名不能为空!");
40             }
41             try
42             {
43                 byte[] buff = Convert.FromBase64String(inputString);
44                 FileInfo fi = new FileInfo(path + filename);
45                 using (FileStream fs2 = fi.OpenWrite())
46                 {
47                     fs2.Write(buff, 0, buff.Length);
48                     return true;
49                 }
50             }
51             catch (Exception ex)
52             {
53                 throw ex;
54             }
55         }

java:

 1 public static void main(String[] args) {
 2         String s = Getstr();
 3         if(GenerateImage(s))
 4             System.out.println("t");
 5         else
 6             System.out.println("f");
 7     }
 8 
 9     private static String Getstr() {
10         String imgFile = "d://test.jpg";
11         InputStream in = null;
12         byte[] data = null;
13         
14         try {
15             in = new FileInputStream(imgFile);
16             data = new byte[in.available()];
17             in.read(data);
18             in.close();
19         } catch (IOException e) {
20             e.printStackTrace();
21         }
22 
23         Base64.Encoder d = Base64.getEncoder();
24         return d.encodeToString(data);
25     }
26 
27     //    base64字符串转化成图片
28     public static boolean GenerateImage(String imgStr) {   
29         if (imgStr == null)
30             return false;
31         Base64.Decoder decoder = Base64.getDecoder();
32         try {
33 
34             byte[] b = decoder.decode(imgStr);
35             for (int i = 0; i < b.length; ++i) {
36                 if (b[i] < 0) {
37                     b[i] += 256;
38                 }
39             }
40 
41             String imgFilePath = "d://new.jpg";
42             OutputStream out = new FileOutputStream(imgFilePath);
43             out.write(b);
44             out.flush();
45             out.close();
46             return true;
47         } catch (Exception e) {
48             return false;
49         }
50     }

ps:import java.util.Base64,这句要在java1.8里才有,如果是1.7或之前版本,可以试试Apache下面的一个Base64的包。

原文地址:https://www.cnblogs.com/imoonstal/p/4292567.html