bug--处理url中带中文异常
This commit is contained in:
@ -24,6 +24,8 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
|
import static co.yixiang.utils.FileUtil.transformStyle;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||||
public class CreatShareProductServiceImpl implements CreatShareProductService {
|
public class CreatShareProductServiceImpl implements CreatShareProductService {
|
||||||
@ -48,7 +50,7 @@ public class CreatShareProductServiceImpl implements CreatShareProductService {
|
|||||||
//读取互联网图片
|
//读取互联网图片
|
||||||
BufferedImage priductUrl = null;
|
BufferedImage priductUrl = null;
|
||||||
try {
|
try {
|
||||||
priductUrl = ImageIO.read(new URL(productDTO.getImage()));
|
priductUrl = ImageIO.read(new URL(transformStyle(productDTO.getImage())));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,8 @@ import java.io.InputStream;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
@ -395,4 +397,58 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
|
|||||||
return buf;
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user