java网络地址上传_Java网络URL文件下载到本地然后通过fastDFS上传网络

@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();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值