Description: The reason to convert to BASE64 is because I am involved in the transfer of cross-system images. Transfer file to another system, and another system to upload files.
Between this, the process from A to B, the picture is present in one of the nodes in the XML. To the B system to do XML to map the process. So in this process, you cannot remove byte[] or file type files from the map. Because the value of the map after the conversion cannot be strongly converted to byte[] or file, it can only be converted to BASE64 string values. To the B end and then to byte[] for file upload operations
public class Filestreamutil {
private static String FolderUrl;
Static Base64encoder encoder = new Sun.misc.BASE64Encoder ();
Static Base64decoder decoder = new Sun.misc.BASE64Decoder ();
To add a picture to a binary stream
public static String fileToBase64 (file file) throws IOException
{
BufferedImage bi;
Bi = imageio.read (file);
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Imageio.write (BI, "JPG", BAOs);
byte[] bytes = Baos.tobytearray ();
Return Encoder.encodebuffer (bytes). Trim ();
}
/**
* Convert binary to byte[]
* @param base64string
*/
public static byte[] Base64stringtoimage (String base64string) {
byte[] bytes = new Byte[base64string.length ()];
try {
bytes = Decoder.decodebuffer (base64string);
return bytes;
} catch (IOException e) {
E.printstacktrace ();
}
return bytes;
}
Convert file to byte[]
public static byte[] Filetobyte (file file) throws IOException
{
BufferedImage bi;
Bi = imageio.read (file);
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Imageio.write (BI, "JPG", BAOs);
byte[] bytes = Baos.tobytearray ();
return bytes;
}
/**
* Write file to target Source: This method temporarily does not
* @param File
*/
public static void WriteFile (file file) {
try {
Byte[] Bytes=filetobyte (file);
Bytearrayinputstream Bais = new Bytearrayinputstream (bytes);
BufferedImage bi1 =imageio.read (Bais); Read source file
Imageio.write (bi1, "JPG", file);//regardless of the output format picture, here do not need to change
} catch (IOException e) {
E.printstacktrace ();
}
}
/**
* Upload files to the server
* @param bytes
*/
public static void Wiretebyte (byte[] bytes) {
String tourl= "";//Destination Address
File W2 = new file (tourl);//Can be jpg,png,gif format "D://base64string.jpg"
try {
Bytearrayinputstream Bais = new Bytearrayinputstream (bytes);
BufferedImage bi1 =imageio.read (Bais); Read source file
Imageio.write (bi1, "JPG", w2);//regardless of the output format picture, here do not need to change
} catch (IOException e) {
Loggerutil.trace ("filestreamutil-----", "Wiretebyte---", "Upload Transport server System error");
E.printstacktrace ();
}
}
/**
* Generate UUID
* Get Fileid
* @return
*/
@SuppressWarnings ("unused")
public static String Getfileid () {
UUID uuid = Uuid.randomuuid ();
String Fileid = Stringutils.replace (uuid.tostring (), "-", "");
return Fileid;
}
/**
* Set NAS path
* @param fileId
* @param uploadfile
* @return
*/
@SuppressWarnings ("unused")
public static string GetFilePath (String fileId) {
String fileType = ". jpg";
String fileName = fileId + fileType;
String datestring = dateformatutil.formatdate (New Date (), "yyyyMMdd");
String Filenaspath = FolderUrl + "/scms/" + datestring + "/" + fileName;
return filenaspath;
}
}
Image upload Related