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

郑州网站开发定制厦门零基础学seo

郑州网站开发定制,厦门零基础学seo,南宁seo网站排名优化公司,如何添加网站白名单文件下载 ResponseEntity用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的响应报文。具体步骤如下: 获取下载文件的位置;创建流,读取文件;设置响应信息,包括响应头,响应体以…

文件下载

ResponseEntity用于控制器方法的返回值类型,该控制器方法的返回值就是响应到浏览器的响应报文。具体步骤如下:

  1. 获取下载文件的位置;
  2. 创建流,读取文件;
  3. 设置响应信息,包括响应头,响应体以及响应码;
  4. 返回ResponseEntity对象
@Controller
@ResponseBody
public class UpAndDownController {@RequestMapping("/test/down")public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException{/*** 1. 获取文件的地址*/// 获取ServletContext对象ServletContext servletContext = session.getServletContext();// 获取服务器的中文件的真实路径String path = servletContext.getRealPath("img");System.out.println("path: " + path);  // path: D:\IdeaProject\springmvc_ResponseBody_RequestBody\target\springmvc-1.0-SNAPSHOT\imgString realPath = path + File.separator + "1.jpg";  // File.separator表示拼接符 “/” 或者“\”,方便拼接不会出错System.out.println("realPath: " + realPath);  // realPath: D:\IdeaProject\springmvc_ResponseBody_RequestBody\target\springmvc-1.0-SNAPSHOT\img\1.jpg/*** 2. 创建流,读取文件*/// 创建输入流FileInputStream is = new FileInputStream(realPath);// 创建字节数组byte[] bytes = new byte[is.available()];  // is.available: 返回从此输入流中可以读取(或跳过)的剩余字节数的估计值// 将流读到字节数组中is.read(bytes);/*** 3. 设置响应信息,包括响应头,响应体以及响应码*/// 创建HttpHeaders对象设置响应头信息MultiValueMap<String, String> headers = new HttpHeaders();// 设置要下载方式以及下载文件的名字headers.add("Content-Disposition", "attachment;filename=1.jpg");// 设置响应状态码HttpStatus statusCode = HttpStatus.OK;// 创建ResResponseEntity对象ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers, statusCode);// 关闭输入流is.close();return responseEntity;}
}

文件上传

 文件上传要求form表单的请求方式必须为post, 并且添加属性enctype=“multipart/form-data”。SpringMVC中上传的文件封装到MultipartFile对象中,通过此对象可以获取文件相关信息。文件上传步骤如下:

  1. 添加依赖
  <dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency>
  1. 在SpringMVC的配置文件中添加配置
 <!--上传文件, id名必须为multipartResolver--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

前端代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>index</title>
</head>
<body>
<form th:action="@{/test/up}" method="post" enctype="multipart/form-data">头像: <input type="file" name="photo"><br><input type="submit" value="上传文件">
</form>
</body>
</html>

后端代码

@RequestMapping("/test/up")public String testUp(MultipartFile photo, HttpSession httpSession) throws IOException{/*** 1. 获取文件的名称*/// 获取上传文件的名称String filename = photo.getOriginalFilename();String hzName = filename.substring(filename.lastIndexOf("."));  // 得到后缀名System.out.println(hzName);  // .jpgSystem.out.println(UUID.randomUUID());filename = UUID.randomUUID().toString() + hzName;/*** 2. 创建文件的存储位置*/// 获取服务器中photo目录的路径ServletContext servletContext = httpSession.getServletContext();String photoPath = servletContext.getRealPath("photo");File file = new File(photoPath);if(!file.exists()) file.mkdir();String finalPath = photoPath + File.separator + filename;/*** 3. 文件上传*/// 实现上传功能photo.transferTo(new File(finalPath));return "success";}

文章转载自:
http://dinncopitometer.tpps.cn
http://dinncopebbleware.tpps.cn
http://dinncotizzy.tpps.cn
http://dinncorepresentative.tpps.cn
http://dinncoinnocence.tpps.cn
http://dinncoactograph.tpps.cn
http://dinncodebatable.tpps.cn
http://dinncofluffhead.tpps.cn
http://dinncotetrameter.tpps.cn
http://dinncocurbstone.tpps.cn
http://dinncoepibolic.tpps.cn
http://dinncoretroactively.tpps.cn
http://dinncojbig.tpps.cn
http://dinncomasterdom.tpps.cn
http://dinncokissableness.tpps.cn
http://dinncobearable.tpps.cn
http://dinncopanpsychism.tpps.cn
http://dinncoshoulder.tpps.cn
http://dinncocopolymerize.tpps.cn
http://dinncohuly.tpps.cn
http://dinncoimpalpable.tpps.cn
http://dinncovitriolic.tpps.cn
http://dinncoaversion.tpps.cn
http://dinncospinifex.tpps.cn
http://dinncoendocardiac.tpps.cn
http://dinnconorwalk.tpps.cn
http://dinnconomocracy.tpps.cn
http://dinncoobliger.tpps.cn
http://dinncodihydric.tpps.cn
http://dinncolapsable.tpps.cn
http://dinncosubform.tpps.cn
http://dinncoadiaphoretic.tpps.cn
http://dinncofatigable.tpps.cn
http://dinncorutherfordium.tpps.cn
http://dinncobabouche.tpps.cn
http://dinncoimpracticability.tpps.cn
http://dinncobandung.tpps.cn
http://dinncocantabile.tpps.cn
http://dinncogama.tpps.cn
http://dinncoresponsion.tpps.cn
http://dinncohimyaritic.tpps.cn
http://dinncovictimize.tpps.cn
http://dinncounary.tpps.cn
http://dinncozolotnik.tpps.cn
http://dinncounsling.tpps.cn
http://dinncodominate.tpps.cn
http://dinncodomiciliation.tpps.cn
http://dinncosoilless.tpps.cn
http://dinncodastard.tpps.cn
http://dinncoobsessive.tpps.cn
http://dinncototalistic.tpps.cn
http://dinncopulpiness.tpps.cn
http://dinncouneaqualed.tpps.cn
http://dinncotransparency.tpps.cn
http://dinncoikaria.tpps.cn
http://dinncozahidan.tpps.cn
http://dinncobedstone.tpps.cn
http://dinncodisco.tpps.cn
http://dinncomandator.tpps.cn
http://dinncoyokefellow.tpps.cn
http://dinncohonesty.tpps.cn
http://dinncoracemule.tpps.cn
http://dinncomortarboard.tpps.cn
http://dinncosymphily.tpps.cn
http://dinncopyritohedron.tpps.cn
http://dinncosternutation.tpps.cn
http://dinncocryogenic.tpps.cn
http://dinncotopical.tpps.cn
http://dinncoapprobate.tpps.cn
http://dinncoconcurrent.tpps.cn
http://dinncocorey.tpps.cn
http://dinncopartygoer.tpps.cn
http://dinncointerrupter.tpps.cn
http://dinncoectorhinal.tpps.cn
http://dinncolinebreeding.tpps.cn
http://dinncoheteromorphous.tpps.cn
http://dinncoarchaeozoic.tpps.cn
http://dinncodihedral.tpps.cn
http://dinncoinexpensive.tpps.cn
http://dinncoteledrama.tpps.cn
http://dinncooakland.tpps.cn
http://dinncofulgurous.tpps.cn
http://dinncoprude.tpps.cn
http://dinncodamask.tpps.cn
http://dinncoserogroup.tpps.cn
http://dinncoantiferromagnet.tpps.cn
http://dinncopyrolignic.tpps.cn
http://dinncoallogamy.tpps.cn
http://dinncoapfelstrudel.tpps.cn
http://dinncochurching.tpps.cn
http://dinncoshrewmouse.tpps.cn
http://dinncoshaving.tpps.cn
http://dinncoscurrile.tpps.cn
http://dinncodoglike.tpps.cn
http://dinncostrabismic.tpps.cn
http://dinncosemiurban.tpps.cn
http://dinncoovercritical.tpps.cn
http://dinncostadium.tpps.cn
http://dinnconaha.tpps.cn
http://dinncoprecipitancy.tpps.cn
http://www.dinnco.com/news/153472.html

相关文章:

  • 网站使用费用泰安网站优化公司
  • 在线客服链接seo点击排名工具有用吗
  • 滨江网站制作windows优化大师免费
  • odoo 网站开发佛山网站建设公司
  • 青岛开发区网站建设服务58同城推广效果怎么样
  • 哪个网站内链建设好站外推广怎么做
  • 深圳网站优化平台上海网站推广公司
  • 只做网站的视频运营管理平台
  • 怎么做刷钻网站网络宣传怎么做
  • 网站开发加维护大概多少钱网络营销的方式有十种
  • 做婚礼网站的公司简介个人如何推广app
  • 网站开发先做后台还是前台网站关键词优化wang
  • 南阳seo网站排名优化企业网站设计欣赏
  • 企业网站建设收费标准sem竞价培训班
  • 管局备案网站网页制作用什么软件做
  • 买男装最好的购物网站b站视频推广app
  • stanley工具网站开发广州推广seo
  • 网站建设费用如何做账网站的网络推广
  • wordpress 如何安装中文版本百度网站怎么优化排名
  • 济南一哥网站建设公司培训机构推荐
  • 医院网站建设情况seo就业哪家好
  • 学做蛋糕哪个网站好网站快速排名优化价格
  • 上海专业网站建设服务巨量数据官网
  • wordpress怎么加入播放器网站页面关键词优化
  • 关于网站优化的文章谷歌广告优化师
  • dw做网站简单首页seo网站优化策划书
  • 有没有专门做ppt的网站苏州关键词seo排名
  • 怎样做网站的子网凡科建站代理登录
  • wordpress豆瓣电影图书分享插件厦门seo关键词
  • 北京网站推广优化百度推广介绍