下载链接中有中文,造成 “400 Bad Request”。
处理方式是,把中文转码。
public class DownAdjunct {
private String urlPath;
private Context context;
private String SDPath = Environment.getExternalStorageDirectory() + "/CableInspection/Download/";
public DownAdjunct(Context context, String urlPath) {
this.urlPath = urlPath;
this.context = context;
}
public String downFile() throws IOException {
String uriPath = "";
// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
String strurl = tranformStyle(urlPath);
URL url = new URL(strurl);
HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
// 获取到文件的大小
int fileSize = conn.getContentLength() / 1024; //KB
System.setProperty("http.keepAlive", "false");
InputStream is;
if (conn.getResponseCode()==200){
is = conn.getInputStream();
}else {
is = conn.getErrorStream();
}
String[] split = urlPath.split("/");
String fileName = SDPath + split[split.length - 1];
File file = new File(fileName);
try {
// 目录不存在创建目录
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
} catch (Exception e) {
// TODO: handle exception
}
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
bis.close();
is.close();
conn.disconnect();
uriPath = file.getAbsolutePath();
} else {
throw new IOException("未发现有SD卡");
}
return uriPath;
}
/**
* 对中文字符进行UTF-8编码
* @param source 要转义的字符串
* @return
* @throws UnsupportedEncodingException
*/
public static String tranformStyle(String source) throws UnsupportedEncodingException
{
char[] arr = source.toCharArray();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < arr.length; i++)
{
char temp = arr[i];
if(isChinese(temp))
{
sb.append(URLEncoder.encode("" + temp, "UTF-8"));
continue;
}
sb.append(arr[i]);
}
return sb.toString();
}
/**
* 获取字符的编码值
* @param s
* @return
* @throws UnsupportedEncodingException
*/
public static int getValue(char s) throws UnsupportedEncodingException
{
String temp = (URLEncoder.encode("" + s, "GBK")).replace("%", "");
if(temp.equals(s + ""))
{
return 0;
}
char[] arr = temp.toCharArray();
int total = 0;
for(int i = 0; i < arr.length; i++)
{
try
{
int t = Integer.parseInt((arr[i] + ""), 16);
total = total * 16 + t;
}
catch(NumberFormatException e)
{
e.printStackTrace();
return 0;
}
}
return total;
}
/**
* 判断是不是中文字符
* @param c
* @return
*/
public static boolean isChinese(char c)
{
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if(ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)
{
return true;
}
return false;
}
}
网友评论