文件上传-pubsec-文件上传大小限制

  1. Caused by: java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
  2. 错误代码:
  3. server:
  4. servlet:
  5. context-path: mozq
  6. 原因:不能以斜杠开头,也不能以斜杠结尾。
  1. # 以斜杠开头的路径表示绝对路径和域名端口号直接拼接。
  2. <form method="post" action="/file/upload" enctype="multipart/form-data">
  3. <input type="file" name="userImg">
  4. <input type="submit" value="提交">
  5. </form>
  6. 实际请求的路径:http://localhost:8080/file/upload 报错。
  7. 项目contextPath=/mozq
  8. 代码:
  9. @RestController
  10. @RequestMapping("/file")
  11. public class FileController {
  12. @RequestMapping("/upload")
  13. public Map<String, Object> upload(MultipartFile file){
  14. return null;
  15. }
  16. }
  17. 方案:
  18. action="file/upload" 不以斜杠开头。使用相对路径。
  1. org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (62488249) exceeds the configured maximum (10485760)
  2. 原因:上传文件大小超过限制。
  3. @Configuration
  4. public class MultiPartConfig {
  5. /**
  6. * 文件上传配置
  7. * @return
  8. */
  9. @Bean
  10. public MultipartConfigElement multipartConfigElement() {
  11. MultipartConfigFactory factory = new MultipartConfigFactory();
  12. // 单个数据大小
  13. factory.setMaxFileSize(DataSize.of(100, DataUnit.MEGABYTES));
  14. /// 总上传数据大小
  15. factory.setMaxRequestSize(DataSize.of(300, DataUnit.MEGABYTES));
  16. return factory.createMultipartConfig();
  17. }
  18. }
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="js/jquery.js"></script>
  7. <style>
  8. .hide{
  9. display: none;
  10. }
  11. .tip{
  12. color: red;
  13. font-size: 12px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <form id="submitForm">
  19. 秘钥文件:<input id="secFile" name="secFile" type="file" onclick="hide('secFile')"><br/>
  20. <div id="secFileCheck" class="hide tip">请选择密钥文件</div>
  21. 公钥文件:<input id="pubFile" name="pubFile" type="file" onclick="hide('pubFile')"><br/>
  22. <div id="pubFileCheck" class="hide tip" >请选择公钥文件</div>
  23. 密码:<input name="password" type="text" onblur="checkPassword()" onclick="hide('password')"><br/>
  24. <div id="passwordCheck" class="hide tip">请输入密码</div>
  25. <input value="提交" type="button" onclick="submitForm()" ><br/>
  26. </form>
  27. <script>
  28. function submitForm() {
  29. var pubFile = document.getElementById("pubFile").files[0];
  30. var secFile = document.getElementById("secFile").files[0];
  31. var password = $("input[name='password']").val();
  32. console.log("私钥文件:");
  33. console.log(secFile);
  34. console.log("公钥文件:");
  35. console.log(pubFile);
  36. console.log("私钥文件密码:" + password);
  37. if(checkFile("pubFile") & checkFile("secFile") & checkPassword()){
  38. var formData = new FormData();
  39. formData.append("pubFile", pubFile);
  40. formData.append("secFile", secFile);
  41. formData.append("password", password);
  42. $.ajax({
  43. url: "isPubAndSecretMatch",
  44. type: "post",
  45. data: formData,
  46. contentType: false,
  47. processData: false,
  48. mimeType: "multipart/form-data",
  49. success: function (data) {
  50. console.log("响应数据:" + data);
  51. data = JSON.parse(data);
  52. if(data.flag == "success"){
  53. alert("公钥和私钥匹配成功");
  54. }else if(data.flag == "fail"){
  55. alert(data.message);
  56. }
  57. }
  58. })
  59. }
  60. }
  61. function hide(ele) {
  62. $("#" + ele + "Check").addClass("hide");
  63. }
  64. function checkFile(fileEleId) {
  65. var fileEle = document.getElementById(fileEleId).files[0];
  66. if(fileEle == null){
  67. $("#" + fileEleId + "Check").removeClass("hide");
  68. return false
  69. }else{
  70. return true;
  71. }
  72. }
  73. function checkPassword() {
  74. var password = $("input[name='password']").val();
  75. if(password == null || password==''){
  76. $("#passwordCheck").removeClass("hide");
  77. return false;
  78. }else{
  79. return true;
  80. }
  81. }
  82. </script>
  83. </body>
  84. </html>
  1. import java.io.BufferedInputStream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import org.springframework.web.multipart.MultipartFile;
  13. /**
  14. * Service class for managing users.
  15. */
  16. @Service
  17. @Transactional
  18. public class FileService {
  19. /***
  20. * 上传
  21. * @param message 记录消息
  22. * @param inputStream 文件流
  23. */
  24. public HashMap<String, Object> uploadQRCode(HttpServletRequest request,MultipartFile file,String path) {
  25. // 检查文件是否上传过 上传成功文件名规则:用户单位_上传文件名称
  26. HashMap<String, Object> uploadResult = new HashMap<String, Object>();
  27. String realPath = request.getServletContext().getRealPath(path);
  28. System.out.println("上传的path:"+realPath);
  29. File temp = new File(realPath);
  30. // 文件夹check
  31. if (!temp.getParentFile().exists() && !temp.getParentFile().isDirectory()) {
  32. temp.getParentFile().mkdirs();
  33. }
  34. try {
  35. // 成功转存数据文件
  36. file.transferTo(temp);
  37. uploadResult.put("QRCodePath", path);
  38. uploadResult.put("success", true);
  39. uploadResult.put("msg", "上传成功");
  40. } catch (Exception e) {
  41. uploadResult.put("success", false);
  42. uploadResult.put("msg", "上传失败:" + e.getMessage());
  43. e.printStackTrace();
  44. }
  45. temp = null;
  46. return uploadResult;
  47. }
  48. /***
  49. * 上传
  50. * @param message 记录消息
  51. * @param inputStream 文件流
  52. */
  53. public Map<String, String> uploadFile(HttpServletRequest request,MultipartFile file,String path) {
  54. // 检查文件是否上传过 上传成功文件名规则:用户单位_上传文件名称
  55. Map<String, String> map= new HashMap<String, String>();
  56. String webpath=path+"/"+ file.getOriginalFilename();
  57. String realPath = request.getServletContext().getRealPath(webpath);
  58. File temp = new File(realPath);
  59. // 文件夹check
  60. if (!temp.getParentFile().exists() && !temp.getParentFile().isDirectory()) {
  61. temp.getParentFile().mkdirs();
  62. }
  63. try {
  64. // 成功转存数据文件
  65. file.transferTo(temp);
  66. map.put("success", "上传成功");
  67. } catch (Exception e) {
  68. map.put("fail", "上传失败:" + e.getMessage());
  69. e.printStackTrace();
  70. }
  71. map.put("webpath", webpath);
  72. temp = null;
  73. return map;
  74. }
  75. /***
  76. * 下载
  77. *
  78. */
  79. public String downLoad(HttpServletRequest request,HttpServletResponse response,String path){
  80. String realPath = request.getServletContext().getRealPath(path);
  81. File file = new File(realPath);
  82. if(file.exists()){ //判断文件父目录是否存在
  83. response.setContentType("application/force-download");// 设置强制下载不打开
  84. response.addHeader("Content-Disposition","attachment;fileName=" + file.getName());// 设置文件名
  85. byte[] buffer = new byte[1024];
  86. FileInputStream fis = null;
  87. BufferedInputStream bis = null;
  88. try {
  89. fis = new FileInputStream(file);
  90. bis = new BufferedInputStream(fis);
  91. OutputStream os = response.getOutputStream();
  92. int i = bis.read(buffer);
  93. while (i != -1) {
  94. os.write(buffer, 0, i);
  95. i = bis.read(buffer);
  96. }
  97. } catch (Exception e) {
  98. e.printStackTrace();
  99. } finally {
  100. if (bis != null) {
  101. try {
  102. bis.close();
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. if (fis != null) {
  108. try {
  109. fis.close();
  110. } catch (IOException e) {
  111. e.printStackTrace();
  112. }
  113. }
  114. }
  115. }
  116. return null;
  117. }
  118. public Map<String, String> uploadFilePic(HttpServletRequest request,MultipartFile file,String path) {
  119. // 检查文件是否上传过 上传成功文件名规则:用户单位_上传文件名称
  120. Map<String, String> map= new HashMap<String, String>();
  121. String realPath = request.getServletContext().getRealPath(path);
  122. File temp = new File(realPath);
  123. // 文件夹check
  124. if (!temp.getParentFile().exists() && !temp.getParentFile().isDirectory()) {
  125. temp.getParentFile().mkdirs();
  126. }
  127. try {
  128. // 成功转存数据文件
  129. file.transferTo(temp);
  130. map.put("success", "上传成功");
  131. } catch (Exception e) {
  132. map.put("fail", "上传失败:" + e.getMessage());
  133. e.printStackTrace();
  134. }
  135. map.put("webpath", path);
  136. temp = null;
  137. return map;
  138. }
  139. }

  1. import java.io.IOException;
  2. import java.util.Map;
  3. import javax.annotation.Resource;
  4. import javax.servlet.http.HttpServletRequest;
  5. import org.apache.log4j.LogManager;
  6. import org.apache.log4j.Logger;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.bind.annotation.ResponseBody;
  12. import org.springframework.web.multipart.MultipartFile;
  13. @Controller
  14. @RequestMapping("/upload")
  15. public class FileUploadController {
  16. private static Logger log = LogManager.getLogger(FileUploadController.class);
  17. private static final String PathRoot = "SystemFile/upload/img";
  18. @Resource
  19. private FileService fileService;
  20. @RequestMapping("/save")
  21. @ResponseBody
  22. public Map<String, String> uploadFile(@RequestParam("file")MultipartFile file,@RequestParam("xxmc")String xxmc, HttpServletRequest request)
  23. throws IllegalStateException, IOException {
  24. log.info("===========进入文件上传方法===============");
  25. if(!file.isEmpty())
  26. {
  27. Map<String, String> map=fileService.uploadFile(request,file,PathRoot+"/"+xxmc);
  28. return map;
  29. }
  30. return null;
  31. }
  32. }

文件上传-pubsec-文件上传大小限制的更多相关文章

  1. 没有选择上传的文件或选择的文件大小超出大小(DEDECMS亲身试验成功)

    dedecms升级到5.7后,后台上传压缩包文件,提示"没有选择上传的文件或选择的文件大小超出大小",由于很久都没弄这个系统了,所以最早怎么设置的也忘记了,就上百度搜,基本上有说的 ...

  2. SpringBoot 文件上传、下载、设置大小

    本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...

  3. input file 文件上传,js控制上传文件的大小和格式

    文件上传一般是用jquery的uploadify,比较好用.后面会出文章介绍uploadify这个插件. 但是,有时候为了偷懒,直接就用input 的file进行文件和图片等的上传,input fil ...

  4. struts2文件上传时获取上传文件的大小

    利用struts2框架上传文件时,如果想要获取上传文件的大小可以利用下面的方式进行: FileInputStream ins = new FileInputStream(file); if (ins. ...

  5. IIS 上传大文件 30MB 设置限制了上传大小

    用uploadify在IIS6下上传大文件没有问题,但是迁移到IIS7下面,上传大文件时,出现HTTP 404错误. 查了半天,原来是IIS7下的默认设置限制了上传大小.这个时候Web.Config中 ...

  6. php 上传文件并对上传的文件进行简单验证(错误信息,格式(防伪装),大小,是否为http上传)

    <body> <?php /** *验证错误 *如果有错,就返回错误,如果没错,就返回null */ function check($file) { //1:验证是否有误 if($f ...

  7. [New Portal]Windows Azure Storage (14) 使用Azure Blob的PutBlock方法,实现文件的分块、离线上传

    <Windows Azure Platform 系列文章目录> 相关内容 Windows Azure Platform (二十二) Windows Azure Storage Servic ...

  8. 【Java EE 学习 22 上】【文件上传】【目录打散】【文件重命名】

    1.文件上传概述 (1)使用<input type="file">的方式来声明一个文件域. (2)表单提交方式一定要是post方式才行 (3)表单属性enctype 默 ...

  9. C#对.CSV格式的文件--逗号分隔值文件 的读写操作及上传ftp服务器操作方法总结

    前言 公司最近开发需要将数据保存到.csv文件(逗号分隔值 文件)中然后上传到ftp服务器上,供我们系统还有客户系统调用,之前完全没有接触过这个,所以先来看看百度的解释:逗号分隔值(Comma-Sep ...

随机推荐

  1. ILRuntime 学习

    ILRuntime: https://github.com/Ourpalm/ILRuntime Demo: https://github.com/Ourpalm/ILRuntimeU3D 中文在线文档 ...

  2. vue 使用localstorage实现面包屑

    mutation.js代码: changeRoute(state, val) { let routeList = state.routeList; let isFind = false; let fi ...

  3. 'GL_EXT_shader_framebuffer_fetch' : extension is not supported

    在使用安卓模拟器加载Flutter应用时, 提示'GL_EXT_shader_framebuffer_fetch' : extension is not supported: D/skia (1404 ...

  4. 【Java语言特性学习之二】反射

    一.概念java加载class文件分两种情况:(1)类型是编译器已知的,这种文件的.class文件在编译的时候,编译器会把.class文件打开(不加载)检查,称为Run- Time Type Iden ...

  5. iOS Workflow 分享 - Scan QR Code

    很多时候我们无意识地用微信扫描一个 QR Code,然后无论打开的是什么我们用就是了.我经常会好奇到底 QR Code 编码的是什么信息,到底是一个 ID 呢,还是一个 URL(可能是 deeplin ...

  6. Qt 编译配置相关总结

    MinGW 与 MSVC 编译的区别 我们可以从 Qt 下载页面看到两种版本编译器,如下图: 我们来对比一下这两个编译器的区别: MSVC 是指微软的 VC 编译器. MinGW 是 Minimali ...

  7. 软件 ---- intelij IDEA安装

    官网下载, 下载地址: https://www.jetbrains.com/idea/download/#section=windows 版本说明:Ultimate 为旗舰版,功能全面,按年收费,这个 ...

  8. redis命令之 ----Set(集合)

    SADD SADD key member [member ...] 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略. 假如 key 不存在,则创 ...

  9. OpenGL入门1.2:渲染管线简介,画三角形

    每一个小步骤的源码都放在了Github 的内容为插入注释,可以先跳过 图形渲染管线简介 在OpenGL的世界里,任何事物是处于3D空间中的,而屏幕和窗口显示的却是2D,所以OpenGL干的事情基本就是 ...

  10. SAP PI接口(RFC类型)在函数字段修改或增加后,出现字段映射错误问题

    在解决标题所言问题之前,我们先回头看看RFC和sproxy这两种接口的优缺点. 关于PI接口的实现,目前我了解到的各大国企项目像中海油.中石化.国网等,普遍实现方式是RFC和代理类sproxy这两种. ...