bug--处理url中带中文异常

This commit is contained in:
taochengbo
2020-06-06 21:06:14 +08:00
parent 1ef1d914ac
commit 2591a13a13
2 changed files with 59 additions and 1 deletions

View File

@ -24,6 +24,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import static co.yixiang.utils.FileUtil.transformStyle;
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CreatShareProductServiceImpl implements CreatShareProductService {
@ -48,7 +50,7 @@ public class CreatShareProductServiceImpl implements CreatShareProductService {
//读取互联网图片
BufferedImage priductUrl = null;
try {
priductUrl = ImageIO.read(new URL(productDTO.getImage()));
priductUrl = ImageIO.read(new URL(transformStyle(productDTO.getImage())));
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -28,6 +28,8 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
@ -395,4 +397,58 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
return buf;
}
/**
* 对中文字符进行UTF-8编码
* @param source 要转义的字符串
* @return
* @throws UnsupportedEncodingException
*/
public static String transformStyle(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 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;
}
}