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

做宠物服务的相关网站设计公司装修哪家好

做宠物服务的相关网站,设计公司装修哪家好,电子商务营销,做付费动漫网站1. 文件上传 1.1 前端注意事项 文件上传操作,前端的表单项需要如下三项设置: (1)input标签的type属性应设置为file,并且注意不要在input标签中设置value属性,因为这可能导致文件上传不成功; …

1. 文件上传

1.1 前端注意事项

        文件上传操作,前端的表单项需要如下三项设置:

        (1)input标签的type属性应设置为file,并且注意不要在input标签中设置value属性,因为这可能导致文件上传不成功;
        (2)请求方式为 method="post" ;

        (3)enctype="multipart/form-data"。

1.2 后端注意事项

        文件上传操作,后端需要注意:

        (1)可以在项目的配置文件 application.properties或application.yml中设置文件大小限制,以避免上传过大的文件导致服务器资源耗尽,例如:

        (2)后端需要写一个处理上传文件的处理器,把接收到的文件保存到服务器指定目录下等其他操作。

1.3 前后端完整示例程序

1.3.1 前端

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head><meta charset="UTF-8"><title>文件上传和下载</title>
</head>
<body>
<form action="/file/upload" method="post" enctype="multipart/form-data">用户名:<input type="text" name="username"/><br/>头 像 :<input type="file" name="headerImg" /><br/>生活照:<input type="file" name="lifeImages" multiple></br><input type="submit" value="提交"/>
</form>
</body>
</html>

1.3.2 后端

package com.shg.springmvc.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.nio.file.Paths;@Controller
public class PageTestController {@RequestMapping(value = "/file/upload", method = RequestMethod.POST)@ResponseBodypublic String fileUpload(@RequestParam(value = "headerImg") MultipartFile file,@RequestParam(value = "lifeImages") MultipartFile[] lifeImages,@RequestParam(value = "username") String userName) throws IOException {System.out.println("用户名:" + userName);System.out.println("头像 - 文件名称:" + file.getOriginalFilename());file.transferTo(Paths.get("D:\\tempPic\\" + file.getOriginalFilename()));for (MultipartFile lifeImage : lifeImages) {System.out.println("生活照 - 文件名称:" + lifeImage.getOriginalFilename());lifeImage.transferTo(Paths.get("D:\\tempPic\\" + file.getOriginalFilename()));}return "success";}
}

2. 文件下载

        文件下载的写法比较固定,代码如下:

package com.shg.springmvc.controller;import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;@RestController
public class ResponseTestController {/*** 问题一:如果文件名称中包含中文,那么浏览器会乱码* 问题二:如果下载的文件比较大,会oom(内存溢出)** @return* @throws IOException*/@RequestMapping(value = "/download", method = RequestMethod.GET)public ResponseEntity<InputStreamResource> response_02() throws IOException {// 获取要下载的文件流String filePath = "D:\\tempPic\\生活照3.jpg";File file = new File(filePath);FileInputStream fileInputStream = new FileInputStream(file);// 解决问题一:使用URLEncoder.encode()方法对文件名称进行编码String fileName = URLEncoder.encode("你好啊.jpg", StandardCharsets.UTF_8);// 解决问题二:为了避免一次性读取文件过大,导致内存溢出,可以使用 InputStreamResource 来读取文件(将文件流包装成Resource对象)InputStreamResource inputStreamResource = new InputStreamResource(fileInputStream);return ResponseEntity.ok()// 设置响应头,告诉浏览器如何处理.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName)// 下载文件的大小.contentLength(file.length())// 内容类型:流.contentType(MediaType.APPLICATION_OCTET_STREAM)// 响应体.body(inputStreamResource);}
}

http://www.yayakq.cn/news/983369/

相关文章:

  • 网站建设与管理 规划书北京的软件公司集中在哪个区
  • 誉重网站建设公司如何建设类似大众点评网站
  • 想自己做微信网站的工作wordpress摘要过滤
  • 做兼职设计去哪个网站好清远网站seo公司
  • 吉安网站制作阿里指数官方网站
  • 课程网站建设规划自助建站基础工作主要包括()
  • 太原网站建设方案服务安徽省建设工程信息网官网首页
  • 西宁建设公司网站免费做网站的方法
  • 传统pc网站外包的优缺点
  • 想做网站找什么公司搜索引擎优化的基本手段
  • 潍坊兆通网站建设win2012服务器做网站
  • 站长是什么级别实用电子商务网站建立
  • 企业网站标签页是什么网页浏览器设置打开
  • 接推广任务的平台成都网站优化方案
  • 做五金奖牌进什么网站做电影网站的工具
  • 苏州网站建设名字常州个人网站设计
  • 什么网站做家具出口做网站的视频教程
  • 阿里云手机做网站成都网站建设优化
  • 逻辑图在线制作网站怎么自己注册网站平台了
  • 企业网站建设(信科网络)国家新闻出版
  • apmserve设置多个网站wordpress路径爆出
  • 怎么夸一个网站做的好看英语网站的建设需要
  • 如何做h5简历制作网站网页版传奇推荐
  • 重庆哪家在做网站建设深圳建设网站哪家好
  • 网站开发项目实例汇总网站推广计划书范文500字
  • 常用的网站都有哪些建筑室内设计网
  • 旅游网站建设步骤在百度上做个网站要多少钱
  • php前台网站开发论坛网站如何备案
  • 网站设计 案例做网站申请域名的流程
  • 微商城网站建设哪家好威海住房建设局网站