我因为要传输MP3文件,但是没有百度到相关的代码,所以自己写了一个,在此处记录一下。废话不多说,上代码。
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.net.URL;
import java.net.URLConnection;
/**
* 总测试方法
* /
public class ToBase64 {
/**
* 总测试方法
* /
public static void main(String[] args) {
// 下载网络文件
int byteread = 0;
String total = null;
byte[] totalbyte = new byte[0];
try {
URL url = new URL("https://网址/文件名.mp3");
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
byte[] buffer = new byte[1204];
while ((byteread = inStream.read(buffer)) != -1) {
//拼接流,这样写是保证文件不会被篡改
totalbyte = byteMerger(totalbyte,buffer,byteread);
}
inStream.close();
} catch (FileNotFoundException