|
|
@ -1,5 +1,6 @@ |
|
|
package cn.chjyj.szwh.utils; |
|
|
package cn.chjyj.szwh.utils; |
|
|
|
|
|
|
|
|
|
|
|
import cn.chjyj.szwh.constant.ChConstant; |
|
|
import com.alibaba.fastjson2.JSON; |
|
|
import com.alibaba.fastjson2.JSON; |
|
|
import com.alibaba.fastjson2.JSONObject; |
|
|
import com.alibaba.fastjson2.JSONObject; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
@ -20,6 +21,8 @@ import org.apache.http.impl.client.HttpClients; |
|
|
import org.apache.http.util.EntityUtils; |
|
|
import org.apache.http.util.EntityUtils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
import java.io.FileOutputStream; |
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.io.InputStream; |
|
|
import java.io.InputStream; |
|
|
import java.net.HttpURLConnection; |
|
|
import java.net.HttpURLConnection; |
|
|
@ -194,4 +197,52 @@ public class RequestUtils { |
|
|
} |
|
|
} |
|
|
return ""; |
|
|
return ""; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 下载发票信息到本地目录 |
|
|
|
|
|
* @param tdurl 发票链接地址 |
|
|
|
|
|
* @param fname 本地存储名称 |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
public static String downTaxTicket(String tdurl,String fname){ |
|
|
|
|
|
String path= ChConstant.INVOICE; //发票文件夹
|
|
|
|
|
|
//按年月日目录分开
|
|
|
|
|
|
Calendar cal = Calendar.getInstance(); |
|
|
|
|
|
int year = cal.get(Calendar.YEAR); |
|
|
|
|
|
int month = cal.get(Calendar.MONTH); |
|
|
|
|
|
int day = cal.get(Calendar.DATE); |
|
|
|
|
|
//检查文件夹是否存在
|
|
|
|
|
|
// 构建形如: upload/pdf/2022/10/20 的目录
|
|
|
|
|
|
String folder = path+ File.separator+year+File.separator |
|
|
|
|
|
+month+File.separator+day; |
|
|
|
|
|
//创建文件夹
|
|
|
|
|
|
File pathFile = new File(folder); |
|
|
|
|
|
if(!pathFile.exists()){ |
|
|
|
|
|
//多级目录方式
|
|
|
|
|
|
pathFile.mkdirs(); |
|
|
|
|
|
} |
|
|
|
|
|
//最终的文件目录
|
|
|
|
|
|
String filename = pathFile+File.separator+fname; |
|
|
|
|
|
//文件下载
|
|
|
|
|
|
CloseableHttpResponse httpResponse = null; |
|
|
|
|
|
CloseableHttpClient httpClient = HttpClients.createDefault(); |
|
|
|
|
|
HttpGet httpGet = new HttpGet(tdurl); |
|
|
|
|
|
try { |
|
|
|
|
|
httpResponse = httpClient.execute(httpGet); |
|
|
|
|
|
HttpEntity entity = httpResponse.getEntity(); |
|
|
|
|
|
if(entity!=null){ |
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(new File(filename)); |
|
|
|
|
|
entity.writeTo(fos); |
|
|
|
|
|
EntityUtils.consume(entity); |
|
|
|
|
|
} |
|
|
|
|
|
//关闭
|
|
|
|
|
|
httpGet.clone(); |
|
|
|
|
|
httpResponse.close(); |
|
|
|
|
|
httpClient.close(); |
|
|
|
|
|
}catch (Exception ex){ |
|
|
|
|
|
logger.error("发票:"+tdurl+",下载出错:"+ex.getMessage()); |
|
|
|
|
|
} |
|
|
|
|
|
return filename; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|