齐齐哈尔做网站宿迁经济技术开发区属于哪个区
- 作者: 五速梦信息网
- 时间: 2026年03月21日 10:12
当前位置: 首页 > news >正文
齐齐哈尔做网站,宿迁经济技术开发区属于哪个区,公众号开发价格多少,最新国际新闻大事件文章目录 文件上传下载文件上传文件下载 文件上传下载 HTTP请求会包含一个请求头#xff0c;其中Content-Type字段告诉服务器正在发送什么类型的数据。根据发送的数据类型#xff0c;浏览器和服务器会采取适应的处理方式。 multipart/form-data是一… 文章目录 文件上传下载文件上传文件下载 文件上传下载 HTTP请求会包含一个请求头其中Content-Type字段告诉服务器正在发送什么类型的数据。根据发送的数据类型浏览器和服务器会采取适应的处理方式。 multipart/form-data是一种常见的POST数据提交的方式。当表单中使用文件上传字段时经常需要使用这个值。表单以multipart/form-data方式发送的数据必须以一种特殊编码方式进行编码使得多个部分可以作为一个整体发送。这种情况常出现在同时发送文件和其他表单数据时。 表单类型有三种常见的MIME类型 “application/x-www-form-urlencoded”这是默认类型适合发送简单的文本数据。“multipart/form-data”适合发送包含所有类型的数据包括文件数据。“text/plain”虽不常用但有其特殊用途用于发送未经编码的文本数据。 文件上传 在HTML表单中如果要提交的数据包含文件类型如图片文档等“enctype属性需要被设置为multipart/form-data”。 1导入依赖 dependencygroupIdcom.liferay/groupIdartifactIdorg.apache.commons.fileupload/artifactIdversion1.2.2.LIFERAY-PATCHED-1/version /dependency2前端界面 !DOCTYPE html htmlheadmeta charsetUTF-8titledemo/titlestyle typetext/cssinput[typesubmit] {outline: none;border-radius: 5px;cursor: pointer;background-color: #31B0D5;border: none;width: 70px;height: 35px;font-size: 20px;}img {border-radius: 50%;}form {position: relative;width: 200px;height: 200px;}input[typefile] {position: absolute;left: 0;top: 0;height: 200px;opacity: 0;cursor: pointer;}/stylescript typetext/javascriptfunction prev(event) {//获取展示图片的区域var img document.getElementById(prevView);//获取文件对象let file event.files[0];//获取文件阅读器let reader new FileReader();reader.readAsDataURL(file);reader.onload function () {//给 img 的 src 设置图片 urlimg.setAttribute(src, this.result);}}/script/headbody!– 表单的 enctype 属性要设置为 multipart/form-data –form action/zzz enctypemultipart/form-data methodpost家居图: img alt height200 idprevView width200input id namepic onchangeprev(this) typefile/家居名: input namename typetextbr/input typesubmit value上传//form/body /html3后端接收并保存 package com.lhs;import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.UUID;public class myServlet02 extends HttpServlet {Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Exception {// 检查请求的enctype标头是否为multipart/form-data这意味着请求包含上载的文件if (ServletFileUpload.isMultipartContent(req)) {// 创建一个新的文件项工厂和文件上传对象DiskFileItemFactory diskFileItemFactory new DiskFileItemFactory();ServletFileUpload servletFileUpload new ServletFileUpload(diskFileItemFactory);// 设置请求头的字符编码为UTF-8解决中文乱码问题servletFileUpload.setHeaderEncoding(utf-8);try {// 解析请求将每个表单字段和文件作为单独的项目ListFileItem list servletFileUpload.parseRequest(req);System.out.println(list);// 迭代这些项目for (FileItem fileItem : list) {// 查看项目是否是一个表单字段还是一个文件 if (!fileItem.isFormField()) {// 定义要上传文件的路径String filePath /upload/;// 计算出文件应该被保存的绝对路径String realPath req.getServletContext().getRealPath(filePath);System.out.println(realPath);// 如果文件路径不存在那么就创建文件路径if (!Files.isDirectory(Path.of(realPath))) {Files.createDirectory(Path.of(realPath));}// 获取文件的原名String name fileItem.getName();// 指定文件的新名字然后写入磁盘fileItem.write(new File(realPath / UUID.randomUUID() _ name));// 设置响应类型和编码然后将成功消息写入响应resp.setContentType(text/html;charsetutf-8);resp.getWriter().write(上传成功);}}} catch (Exception e) {throw new RuntimeException(e);}} else {System.out.println(不是表单);}}Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }4文件按日期分组存储 //获取当前本地日期 LocalDate localDate LocalDate.now();//从日期中获取年份 int year localDate.getYear();//从日期中获取月份 int month localDate.getMonth().getValue();//从日期中获得日期 int day localDate.getDayOfMonth();//通过年、月、日构造文件路径字符串 String filePath /upload/ year / month / day /;//获取此文件路径的真实路径 String realPath req.getServletContext().getRealPath(filePath);if (!Files.isDirectory(Path.of(realPath))) {//创建该路径下的所有目录Files.createDirectories(Path.of(realPath)); }文件下载 在Http响应中当Content-Disposition的值设置为attachment那么浏览器通常会尝试下载并保存的响应数据而不是直接在浏览器中显示它。 1前端界面 !DOCTYPE html htmlheadmeta charsetUTF-8titledemo/title/headbodyh1文件下载/h1a href/zzz?name1.png点击下载图片1/abr/bra href/zzz?name2.png点击下载图片2/abr/br/body /html2将资源存放在web/download目录下 3后端返回资源 package com.lhs;import org.apache.commons.io.IOUtils;import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder;public class myServlet02 extends HttpServlet {Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Exception {// 设置请求编码格式为utf-8req.setCharacterEncoding(utf-8);// 获取下载资源名String name req.getParameter(name);// 构建文件的完整路径String fullPath /download/ name;ServletContext servletContext req.getServletContext();// 获取文件的MIME类型String mimeType servletContext.getMimeType(fullPath);// 设置响应的内容类型resp.setContentType(mimeType);// 设置响应头使得浏览器接收到这个响应后会下载文件而不是直接打开// filename参数指定下载后的文件名这里需要进行URL编码以防文件名有特殊字符resp.setHeader(Content-Disposition, attachment; filename URLEncoder.encode(name));// 获取要下载的文件的输入流InputStream resourceAsStream servletContext.getResourceAsStream(fullPath);// 获取响应的输出流ServletOutputStream outputStream resp.getOutputStream();// 使用Apache的IOUtils工具类把文件的输入流复制到响应输出流实现文件下载IOUtils.copy(resourceAsStream, outputStream);}Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }
- 上一篇: 齐齐哈尔北京网站建设seo管理系统培训运营
- 下一篇: 祁阳做网站网站建设程序员做什么
相关文章
-
齐齐哈尔北京网站建设seo管理系统培训运营
齐齐哈尔北京网站建设seo管理系统培训运营
- 技术栈
- 2026年03月21日
-
齐诺网站建设东莞网站建设做网站长春网站制作公司哪个好
齐诺网站建设东莞网站建设做网站长春网站制作公司哪个好
- 技术栈
- 2026年03月21日
-
齐河县城乡建设局官方网站wordpress和worldpress
齐河县城乡建设局官方网站wordpress和worldpress
- 技术栈
- 2026年03月21日
-
祁阳做网站网站建设程序员做什么
祁阳做网站网站建设程序员做什么
- 技术栈
- 2026年03月21日
-
祁阳做网站网站怎么关闭
祁阳做网站网站怎么关闭
- 技术栈
- 2026年03月21日
-
其它区便宜营销型网站建设南县网站建设推荐
其它区便宜营销型网站建设南县网站建设推荐
- 技术栈
- 2026年03月21日






