URLコードの問題
1754 ワード
Javaを使用してファイルをダウンロードすると、アドレスが認識されない可能性があります.コードの問題が原因です.
上の
エラーが発生する可能性があります.
URLには中国語は含まれてはいけません. 中国語が含まれている場合は、中国語文字をバイトにする(GB 18030やUTF-8などの符号化を利用する).
クライアントの変換の方式はサーバーと同じでなければならなくて、例えばサーバーはURLの中の中国語がUTF-8によって符号化すると思って、あなたのクライアントはGB 18030によって符号化することができません.
Tomcatの場合はserver.xmlを変更する必要があります.インターネットで「tomcat URIEncoding」というキーワードで検索し、URIEncodingというものを修正します.
クライアントの場合は次のように変更できます.
私はこのように解決しました.問題があればいつでも提出します.どういたしまして.
public void DownloadFile(URL theURL, String filePath,String fileFullName) throws IOException {
URLConnection con = theURL.openConnection();
if (fileFullName != null) {
byte[] buffer = new byte[4 * 1024];
int read;
String path = filePath + "/" + fileFullName;
File fileFolder = new File(filePath);
if(!fileFolder.exists()){
fileFolder.mkdir();
}
InputStream in = con.getInputStream();
FileOutputStream os = new FileOutputStream(path);
while ((read = in.read(buffer)) > 0) {
os.write(buffer, 0, read);
}
os.close();
in.close();
this.onStopFile(FILE_URL,"add",fileFullName,path);
}else{
Log.i("DownloadFile", "ERROR");
}
}
上の
InputStream in = con.getInputStream();
エラーが発生する可能性があります.
URLには中国語は含まれてはいけません. 中国語が含まれている場合は、中国語文字をバイトにする(GB 18030やUTF-8などの符号化を利用する).
クライアントの変換の方式はサーバーと同じでなければならなくて、例えばサーバーはURLの中の中国語がUTF-8によって符号化すると思って、あなたのクライアントはGB 18030によって符号化することができません.
Tomcatの場合はserver.xmlを変更する必要があります.インターネットで「tomcat URIEncoding」というキーワードで検索し、URIEncodingというものを修正します.
クライアントの場合は次のように変更できます.
String urlS = "http://172.18.33.133:8080/zh/ .jpg";
String host="http://172.18.33.133:8080/zh/";
String filename=java.net.Encoder.encode(" .jpg"," 。");
String urlS = host+filename;
URL theURL = new URL(urlS);
私はこのように解決しました.問題があればいつでも提出します.どういたしまして.