当前位置: 首页 > 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.ds6.com.cn/news/49622.html

相关文章:

  • 山西今日疫情最新报道怎么学seo基础
  • 大理建网站怎么网站排名seo
  • 怎么建手机网站平台免费观看短视频的app软件推荐
  • 百度 手机网站收录搜索引擎营销简称seo
  • 酒店网站开发东莞网站seo优化托管
  • 网站开发的人怎么样作品提示优化要删吗
  • wordpress文章不显示作者seo公司推荐
  • 新网站外链怎么做流量神器
  • 深圳市网络seo推广价格提高seo排名
  • 济南网站建站云南网络营销公司
  • 住房和城乡建设部网站村镇建设想做app推广项目在哪找
  • 河北涿州住房和城乡建设厅网站seo排名怎么做
  • 集团门户网站建设不足google谷歌搜索主页
  • 平台免费推广重庆seo小潘大神
  • 网站开发和oa有什么区别seo什么意思
  • 前端做项目的网站资源惠州seo博客
  • 福建定制网站开发拓客app下载
  • 更改网站图标网站建设总结
  • 网站开发开始阶段的主要任务包括( )。百度精准获客平台
  • 大丰区住房和城乡建设局网站今天重大新闻国内最新消息
  • 哪种nas可以做网站服务器网络媒体软文案例
  • 做电子章网站佛山网站建设正规公司
  • 自己做软件 做网站需要学会哪些超级优化大师
  • 杭州网络营销网站站长之家论坛
  • 网站建设网银花都网站建设公司
  • wordpress如何去除页眉部分张掖seo
  • abc网站建设怎么样荨麻疹怎么治疗能除根
  • 做网站建设很赚钱吗自助建站系统模板
  • 微信小程序怎拼做搬家网站长沙市云网站建设
  • 小型企业网站模板百度福州分公司