@PostMapping("/updateAttachment")
public void updateAttachment() throws IOException, URISyntaxException {
EntityWrapper entityWrapper = new EntityWrapper();
entityWrapper.like("att_url","%123%");
List attList = sysAttachmentService.selectList(entityWrapper);
for(SysAttachment att : attList){
String empNo = "";
File savePath = new File("D:\\picture\\");
File file = new File(savePath + "\\" + att.getAttViewName());//创建新文件
try {
if (file != null && !file.exists()) {
file.createNewFile();
}
OutputStream oputstream = new FileOutputStream(file);
URL url = new URL(att.getAttUrl());
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);//设置是否要从 URL 连接读取数据,默认为true
uc.connect();
InputStream iputstream = uc.getInputStream();
System.out.println("file size is:" + uc.getContentLength());//打印文件长度
byte[] buffer = new byte[4 * 1024];
int byteRead = -1;
while ((byteRead = (iputstream.read(buffer))) != -1) {
oputstream.write(buffer, 0, byteRead);
}
oputstream.flush();
iputstream.close();
oputstream.close();
}catch (Exception e){
log.info(e.getMessage()+e.getLocalizedMessage());
}
Map resultMap = new HashMap<>();
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = "";
try {
String singleUploadUrl = SystemUtil.getValueByKey("singleUploadUrl");
HttpPost httpPost = new HttpPost(singleUploadUrl);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(Charset.forName("UTF-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", new FileInputStream(file), ContentType.MULTIPART_FORM_DATA, att.getAttViewName());// 文件流
builder.addTextBody("empno",empNo);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);// 执行提交
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
JSONObject jsStr = JSONObject.parseObject(result);
String object = jsStr.getString("object");
JSONObject jsObject = JSONObject.parseObject(object);
AttackmentDto dto = new AttackmentDto();
dto.setMsg("上传成功");
dto.setId(att.getId());
BeanUtil.copyProperties(dto,att);
sysAttachmentService.updateById(att);
}
} catch (Exception e) {
e.getMessage();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}