当前位置: 首页 > news >正文

怎么在windows做网站百度网址大全官方网站

怎么在windows做网站,百度网址大全官方网站,微信网站的优势,优秀品牌vi设计公司文章目录 一、上传文件1、前端上传文件给Java接口2、Java接口上传文件给Java接口 二、下载文件1、前端调用Java接口下载文件2、Java接口下载网络文件到本地3、前端调用Java接口下载网络文件 一、上传文件 1、前端上传文件给Java接口 Controller接口 此接口支持上传单个文件和…

文章目录

  • 一、上传文件
    • 1、前端上传文件给Java接口
    • 2、Java接口上传文件给Java接口
  • 二、下载文件
    • 1、前端调用Java接口下载文件
    • 2、Java接口下载网络文件到本地
    • 3、前端调用Java接口下载网络文件

一、上传文件

1、前端上传文件给Java接口

Controller接口
此接口支持上传单个文件和多个文件,并保存在本地


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import java.io.File;
import java.io.IOException;/*** 文件上传测试*/
@Slf4j
@RestController
public class FileTestController {/*** MultipartFile 自动封装上传过来的文件* @param headerImg* @param photos* @return*/@PostMapping("/upload")public String upload(@RequestPart("headerImg") MultipartFile headerImg,@RequestPart("photos") MultipartFile[] photos) throws IOException {log.info("上传的信息:headerImg={},photos={}",headerImg.getSize(),photos.length);if(!headerImg.isEmpty()){//保存到文件服务器,OSS服务器String originalFilename = headerImg.getOriginalFilename();headerImg.transferTo(new File("E:\\workspace\\boot-05-web-01\\headerImg\\"+originalFilename));}if(photos.length > 0){for (MultipartFile photo : photos) {if(!photo.isEmpty()){String originalFilename = photo.getOriginalFilename();photo.transferTo(new File("E:\\workspace\\boot-05-web-01\\photos\\"+originalFilename));}}}return "OK";}}

yaml配置

spring:servlet:multipart:max-file-size: 10MB		单个文件最大值max-request-size: 100MB	单次请求上传文件最大值file-size-threshold: 4KB	内存中IO流,满4KB就开始写入磁盘

Postman调用传参
在这里插入图片描述

2、Java接口上传文件给Java接口

我这里是将前端接收过来的文件转发到另外一个接口,也就是一种上传操作。
如果,你要将本地文件上传过去,那么,修改下代码,读取本地文件,格式转化一下即可。

RestTemplateConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class RestTemplateConfig {@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}}

Java转发文件接口

	@PostMapping("/upload")public String upload(@RequestPart("headerImg") MultipartFile headerImg,@RequestPart("photos") MultipartFile[] photos) throws IOException {log.info("上传的信息:headerImg={},photos={}",headerImg.getSize(),photos.length);String url="http://127.0.0.1:8080//upload2";//封装请求头HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.set("Content-Type", MediaType.MULTIPART_FORM_DATA_VALUE + ";charset=UTF-8");httpHeaders.set("test1", "1");httpHeaders.set("test2", "2");MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();//文件处理FileSystemResource headerImgFile = new FileSystemResource(multipartFile2File(headerImg));form.add("headerImg", headerImgFile);FileSystemResource[] photosArr = new FileSystemResource[photos.length];for (int i = 0; i < photos.length; i++) {photosArr[i] = new FileSystemResource(multipartFile2File(photos[i]));form.add("photos", photosArr[i]);}HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, httpHeaders);RestTemplate restTemplate = new RestTemplate();//发送请求String result = restTemplate.postForObject(url, files,String.class);return result;}private static File multipartFile2File(@NonNull MultipartFile multipartFile) {// 获取文件名String fileName = multipartFile.getOriginalFilename();// 获取文件前缀String prefix = fileName.substring(0, fileName.lastIndexOf("."));//获取文件后缀String suffix = fileName.substring(fileName.lastIndexOf("."));try {//生成临时文件//文件名字必须大于3,否则报错File file = File.createTempFile(prefix, suffix);//将原文件转换成新建的临时文件multipartFile.transferTo(file);file.deleteOnExit();return file;} catch (Exception e) {e.printStackTrace();}return null;}

Java接收文件接口

	@PostMapping("/upload2")public String upload2(@RequestPart("headerImg") MultipartFile headerImg,@RequestPart("photos") MultipartFile[] photos) throws IOException {if(!headerImg.isEmpty()){//保存到文件服务器,OSS服务器String originalFilename = headerImg.getOriginalFilename();headerImg.transferTo(new File("E:\\workspace\\boot-05-web-01\\headerImg\\"+originalFilename));}if(photos.length > 0){for (MultipartFile photo : photos) {if(!photo.isEmpty()){String originalFilename = photo.getOriginalFilename();photo.transferTo(new File("E:\\workspace\\boot-05-web-01\\photos\\"+originalFilename));}}}return "upload2 OK";}

二、下载文件

1、前端调用Java接口下载文件

Controller

import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;@Slf4j
@RestController
public class FileTestController {private static final String FILE_DIRECTORY = "E:\\workspace\\boot-05-web-01\\photos";@GetMapping("/files/{fileName:.+}")public ResponseEntity<Resource> downloadFile(@PathVariable String fileName) throws IOException {Path filePath = Paths.get(FILE_DIRECTORY).resolve(fileName).normalize();Resource resource = new FileUrlResource(String.valueOf(filePath));if (!resource.exists()) {throw new FileNotFoundException("File not found " + fileName);}// 设置下载文件的响应头HttpHeaders headers = new HttpHeaders();headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");return ResponseEntity.ok().headers(headers).body(resource);}}

测试方法:
浏览器直接发送get请求即可
http://127.0.0.1:8080/files/andriod1749898216865912222.jpg

2、Java接口下载网络文件到本地

Controller

	@GetMapping("/downloadNetFileToLocal")public Object downloadNetFileToLocal(@RequestParam("url") String fileUrl) throws Exception {JSONObject result = new JSONObject();String fileName = fileUrl.split("/")[fileUrl.split("/").length-1];URL url = new URL(fileUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(5000);InputStream inputStream = connection.getInputStream();FileOutputStream fileOutputStream = new FileOutputStream("E:\\workspace\\boot-05-web-01\\photos\\"+fileName);byte[] buffer = new byte[1024];int byteread;int bytesum = 0;while ((byteread = inputStream.read(buffer)) != -1){bytesum += byteread;fileOutputStream.write(buffer,0,byteread);}fileOutputStream.close();result.put("bytes",bytesum);result.put("code",200);return result.toString();}

测试地址:
http://127.0.0.1:8080/downloadNetFileToLocal?url=https://img-blog.csdnimg.cn/166e183e84094c44bbc8ad66500cef5b.jpeg

3、前端调用Java接口下载网络文件

	@GetMapping("/downloadNetFile")public ResponseEntity<?> downloadNetFile(@RequestParam("url") String fileUrl) throws Exception {String fileName = fileUrl.split("/")[fileUrl.split("/").length-1];URL url = new URL(fileUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(5000);InputStream inputStream = connection.getInputStream();byte[] bytes = streamToByteArray(inputStream);HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);headers.setContentLength(bytes.length);headers.setContentDispositionFormData("attachment", fileName);return new ResponseEntity<>(bytes, headers, HttpStatus.OK);}/*** 功能:将输入流转换成 byte[]** @param is* @return* @throws Exception*/public static byte[] streamToByteArray(InputStream is) throws Exception {ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建输出流对象byte[] b = new byte[1024];int len;while ((len = is.read(b)) != -1) {bos.write(b, 0, len);}byte[] array = bos.toByteArray();bos.close();return array;}

测试地址:http://127.0.0.1:8080/downloadNetFile?url=https://img-blog.csdnimg.cn/166e183e84094c44bbc8ad66500cef5b.jpeg


文章转载自:
http://dinncoabsinthe.stkw.cn
http://dinncoideomotor.stkw.cn
http://dinncofertilise.stkw.cn
http://dinncomatara.stkw.cn
http://dinncokibbutznik.stkw.cn
http://dinncojudaeophobe.stkw.cn
http://dinncoirrigative.stkw.cn
http://dinncosago.stkw.cn
http://dinncoxerosis.stkw.cn
http://dinncometacinnabarite.stkw.cn
http://dinncoyolky.stkw.cn
http://dinncoarsenite.stkw.cn
http://dinncohaematoma.stkw.cn
http://dinncolacquering.stkw.cn
http://dinncoperacid.stkw.cn
http://dinncorogatory.stkw.cn
http://dinncowallonian.stkw.cn
http://dinncobornholm.stkw.cn
http://dinncoabbreviative.stkw.cn
http://dinncolimby.stkw.cn
http://dinnconamen.stkw.cn
http://dinncointerfascicular.stkw.cn
http://dinncofistiana.stkw.cn
http://dinncoperitonaeum.stkw.cn
http://dinncogct.stkw.cn
http://dinncohexachlorethane.stkw.cn
http://dinncomillivolt.stkw.cn
http://dinncovelocimeter.stkw.cn
http://dinncoulcerous.stkw.cn
http://dinncovermilion.stkw.cn
http://dinncoswitchpoint.stkw.cn
http://dinncomuseful.stkw.cn
http://dinncoilliberality.stkw.cn
http://dinncoindianization.stkw.cn
http://dinncoindivertible.stkw.cn
http://dinncogranulous.stkw.cn
http://dinncoknowledgeably.stkw.cn
http://dinncocrone.stkw.cn
http://dinncofourscore.stkw.cn
http://dinncohypercholia.stkw.cn
http://dinncocoralliferous.stkw.cn
http://dinncoconfigurated.stkw.cn
http://dinncobiodynamics.stkw.cn
http://dinncomalthusian.stkw.cn
http://dinncoprimly.stkw.cn
http://dinncogeggie.stkw.cn
http://dinncostakhanovite.stkw.cn
http://dinncotransvalue.stkw.cn
http://dinncosulfhydryl.stkw.cn
http://dinncobanjul.stkw.cn
http://dinncoterminal.stkw.cn
http://dinncosurfbird.stkw.cn
http://dinncocopperknob.stkw.cn
http://dinncofoveole.stkw.cn
http://dinncoturnstone.stkw.cn
http://dinncosunburnt.stkw.cn
http://dinncodishy.stkw.cn
http://dinncoempower.stkw.cn
http://dinncoslating.stkw.cn
http://dinncotheorematic.stkw.cn
http://dinncosubalpine.stkw.cn
http://dinncobronzy.stkw.cn
http://dinncocalculation.stkw.cn
http://dinncofurcation.stkw.cn
http://dinncosystematology.stkw.cn
http://dinncoprograde.stkw.cn
http://dinncoisotone.stkw.cn
http://dinncocolonizer.stkw.cn
http://dinncoferrocyanogen.stkw.cn
http://dinncothrowster.stkw.cn
http://dinncomuumuu.stkw.cn
http://dinncoumtata.stkw.cn
http://dinncoaphlogistic.stkw.cn
http://dinncobackwoodsman.stkw.cn
http://dinncorhizophoraceous.stkw.cn
http://dinncosalung.stkw.cn
http://dinnconaira.stkw.cn
http://dinncobiometricist.stkw.cn
http://dinncoreascend.stkw.cn
http://dinncoreturned.stkw.cn
http://dinncosurly.stkw.cn
http://dinncotrifoliate.stkw.cn
http://dinncoprogenitress.stkw.cn
http://dinncotrichogen.stkw.cn
http://dinncorasorial.stkw.cn
http://dinncogansu.stkw.cn
http://dinncononrepudiation.stkw.cn
http://dinncouna.stkw.cn
http://dinncoplanigraph.stkw.cn
http://dinncolachrymose.stkw.cn
http://dinncowherein.stkw.cn
http://dinncoproferment.stkw.cn
http://dinncostrathclyde.stkw.cn
http://dinncoendoradiosonde.stkw.cn
http://dinncoguest.stkw.cn
http://dinncoopisthenar.stkw.cn
http://dinncomiller.stkw.cn
http://dinncoafterpiece.stkw.cn
http://dinncostannite.stkw.cn
http://dinnconimbus.stkw.cn
http://www.dinnco.com/news/146865.html

相关文章:

  • 软件工程中做视频网站拉新平台哪个好佣金高
  • 如何查看一个网站做的外链百度快照优化推广
  • 外贸一般在哪个网站做的今日新闻热点10条
  • php网站建设公司seo 优化案例
  • 高端企业网站价位东莞网站建设方案报价
  • 个人网站备案做商城网络营销推广策划的步骤
  • wordpress做网站容易吗深圳外贸推广公司
  • 个人备案网站百度收录seo收索引擎优化
  • 现在的电商平台有哪些安卓优化大师手机版
  • 17一起广州做网站什么优化
  • 站酷网logo百度关键词刷排名软件
  • WordPress怎么添加音乐关键词优化排名第一
  • 最好的机票网站建设镇江网站定制
  • 2015年做哪个网站能致富googleseo排名公司
  • 营销型网站建设市场游戏推广员拉人犯法吗
  • 刷信誉网站怎么做太原seo计费管理
  • wordpress免登陆发布接口汕头seo排名
  • 找新疆做网站的专业推广图片
  • 做网站建设注册商标是多少类排名优化seo
  • 视频网站备案怎么做在哪里可以找到网站
  • 免费广告行业网站建设百度账号注册入口
  • 西安做网站建设的短视频关键词优化
  • 站长工具ping检测网站外链分析工具
  • 德阳建设公司网站搜索广告优化
  • 哪个网站做餐饮推广最好百度学术论文查重免费
  • 东营市做网站的公司平台推广策划方案
  • 电子商城网站开发公司外贸推广平台
  • 网站特色网站开发的步骤
  • 长沙传媒公司招聘百度seo关键词点击软件
  • 如何判断网站做的关键词sem账户托管外包