問題の説明:
画像の URL から画像をダウンロードし、ローカル操作は正常ですが、サーバーに展開した後、エラーが報告され、エラー コード 400 が報告されます。
メソッドコード:
/**
* 通过图片url下载图片到指定文件夹
* @param downloadUrl 图片url
*/
public void downloadFile(String downloadUrl,String fileName) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
//获取连接
downloadUrl = URLEncoder.encode(downloadUrl, "UTF-8");
downloadUrl= URLDecoder.decode(downloadUrl, "UTF-8");
URL url=new URL(downloadUrl);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setConnectTimeout(3*1000);
//设置请求头
connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36");
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
//获取输入流
inputStream=connection.getInputStream();
File fileDir=new File(downloadDir);
if(!fileDir.exists()){//如果文件夹不存在
fileDir.mkdir();//创建文件夹
}
//截取文件名称,可以把 / 换成需要的规则
String filePath = downloadDir+"/" + downloadUrl.substring(downloadUrl.lastIndexOf("/"));
File file = new File(filePath);
file.renameTo(new File(fileName));
file.createNewFile();//创建文件,存在覆盖
outputStream = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
logger.error("文件下载出错:" + e);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
logger.error("关闭流出错:" + e);
}
}
}
エラーメッセージ:
java.io.IOException: Server returned HTTP response code: 400 for URL: https://file.sfaq.com.cn/insurance/货物运输企业安全_CN2023041718230168_1681786990862.docx
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at org.jeecg.modules.util.FileDownloadUtil.downloadFile(FileDownloadUtil.java:94)
エラーコード行:
//获取输入流
inputStream=connection.getInputStream();
アプローチ:
最初に見つけたのはURLリンクに問題があった.encodeメソッドを使うとコードが再デコードされている.これは問題ではないことを示している.スタックオーバーフローで見つかったメソッド:ソースアドレス:スタックオーバーフロー
回答アドレス
エラー行を修正した後、正常にダウンロードして使用できます。修正内容は次のとおりです。
//获取输入流 服务器上报错400,需判断获取数据流
//inputStream=connection.getInputStream();
if (connection.getResponseCode() >= 400) {
inputStream = connection.getErrorStream();
} else {
inputStream = connection.getInputStream();
}
この方法を使用して問題を解決し、将来忘れないように、いくつかの方法を試しました。記録してください。より良い方法があれば、追加して修正してください〜
2023-04-19 更新
エラーの原因が判明!! ! ! ! URL リンクに漢字が含まれているため、単一のファイルをダウンロードする場合はエラーは発生しませんが、リンクを介してサーバーから複数のファイルをダウンロードし、zip ファイルに圧縮するとエラーが報告されるため、すべてのアップロード方法は pinyin4j パッケージで使用されます convertChineseToPinyin メソッドは中国語をピンインに変換し、通常どおり使用できます〜
<!--汉字转拼音-->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
String orgName = file.getOriginalFilename();
orgName = CommonUtils.convertChineseToPinyin(orgName,true, HanyuPinyinCaseType.LOWERCASE);