【发布时间】:2015-07-16 17:20:47
【问题描述】:
我想使用InputStream 和FileOutputStream 下载文件。我的代码如下所示:
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
con.setRequestProperty("Cache-Control", "no-cache");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
try {
InputStream inputStream = con.getInputStream();
FileOutputStream outputStream = new FileOutputStream("C:\\programs\\TRYFILE.csv");
int bytesRead = -1;
byte[] buffer = new byte[4096];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch(Exception e) {
//
} finally {
outputStream.close();
inputStream.close();
}
代码运行良好并且可以下载文件。但我想知道,如果文件包含土耳其语字符(ş、Ğ、Ç、İ、Ö 等),此代码是否会下载包含这些字符的文件?因此,我想下载包含这些字符(如果包含)的文件,并在我的文件中查看这些字符未触及。
那么,这段代码是否适用于 UTF-8?
【问题讨论】:
-
您正在传输字节不变,因此,结果将是源使用的任何编码。如果连接使用
UTF-8,则结果将为UTF-8。
标签: java file utf-8 fileoutputstream utf