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

Caused by: java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
错误代码:
server:
servlet:
context-path: mozq
原因:不能以斜杠开头,也不能以斜杠结尾。
# 以斜杠开头的路径表示绝对路径和域名端口号直接拼接。
<form method="post" action="/file/upload" enctype="multipart/form-data">
<input type="file" name="userImg">
<input type="submit" value="提交">
</form>
实际请求的路径:http://localhost:8080/file/upload 报错。
项目contextPath=/mozq
代码:
@RestController
@RequestMapping("/file")
public class FileController {
@RequestMapping("/upload")
public Map<String, Object> upload(MultipartFile file){
return null;
}
}
方案:
action="file/upload" 不以斜杠开头。使用相对路径。
org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (62488249) exceeds the configured maximum (10485760)
原因:上传文件大小超过限制。 @Configuration
public class MultiPartConfig {
/**
* 文件上传配置
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
// 单个数据大小
factory.setMaxFileSize(DataSize.of(100, DataUnit.MEGABYTES));
/// 总上传数据大小
factory.setMaxRequestSize(DataSize.of(300, DataUnit.MEGABYTES));
return factory.createMultipartConfig();
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery.js"></script>
<style>
.hide{
display: none;
}
.tip{
color: red;
font-size: 12px;
}
</style>
</head>
<body> <form id="submitForm">
秘钥文件:<input id="secFile" name="secFile" type="file" onclick="hide('secFile')"><br/>
<div id="secFileCheck" class="hide tip">请选择密钥文件</div>
公钥文件:<input id="pubFile" name="pubFile" type="file" onclick="hide('pubFile')"><br/>
<div id="pubFileCheck" class="hide tip" >请选择公钥文件</div>
密码:<input name="password" type="text" onblur="checkPassword()" onclick="hide('password')"><br/>
<div id="passwordCheck" class="hide tip">请输入密码</div>
<input value="提交" type="button" onclick="submitForm()" ><br/>
</form> <script> function submitForm() { var pubFile = document.getElementById("pubFile").files[0];
var secFile = document.getElementById("secFile").files[0];
var password = $("input[name='password']").val();
console.log("私钥文件:");
console.log(secFile);
console.log("公钥文件:");
console.log(pubFile);
console.log("私钥文件密码:" + password); if(checkFile("pubFile") & checkFile("secFile") & checkPassword()){
var formData = new FormData();
formData.append("pubFile", pubFile);
formData.append("secFile", secFile);
formData.append("password", password); $.ajax({
url: "isPubAndSecretMatch",
type: "post",
data: formData,
contentType: false,
processData: false,
mimeType: "multipart/form-data",
success: function (data) {
console.log("响应数据:" + data);
data = JSON.parse(data);
if(data.flag == "success"){
alert("公钥和私钥匹配成功");
}else if(data.flag == "fail"){
alert(data.message);
}
}
})
}
} function hide(ele) {
$("#" + ele + "Check").addClass("hide");
} function checkFile(fileEleId) {
var fileEle = document.getElementById(fileEleId).files[0];
if(fileEle == null){
$("#" + fileEleId + "Check").removeClass("hide");
return false
}else{
return true;
}
} function checkPassword() {
var password = $("input[name='password']").val();
if(password == null || password==''){
$("#passwordCheck").removeClass("hide");
return false;
}else{
return true;
}
}
</script>
</body>
</html>
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; /**
* Service class for managing users.
*/
@Service
@Transactional
public class FileService {
/***
* 上传
* @param message 记录消息
* @param inputStream 文件流
*/
public HashMap<String, Object> uploadQRCode(HttpServletRequest request,MultipartFile file,String path) { // 检查文件是否上传过 上传成功文件名规则:用户单位_上传文件名称
HashMap<String, Object> uploadResult = new HashMap<String, Object>();
String realPath = request.getServletContext().getRealPath(path);
System.out.println("上传的path:"+realPath);
File temp = new File(realPath);
// 文件夹check
if (!temp.getParentFile().exists() && !temp.getParentFile().isDirectory()) {
temp.getParentFile().mkdirs();
}
try {
// 成功转存数据文件
file.transferTo(temp);
uploadResult.put("QRCodePath", path);
uploadResult.put("success", true);
uploadResult.put("msg", "上传成功");
} catch (Exception e) {
uploadResult.put("success", false);
uploadResult.put("msg", "上传失败:" + e.getMessage());
e.printStackTrace();
} temp = null;
return uploadResult;
} /***
* 上传
* @param message 记录消息
* @param inputStream 文件流
*/
public Map<String, String> uploadFile(HttpServletRequest request,MultipartFile file,String path) {
// 检查文件是否上传过 上传成功文件名规则:用户单位_上传文件名称
Map<String, String> map= new HashMap<String, String>();
String webpath=path+"/"+ file.getOriginalFilename();
String realPath = request.getServletContext().getRealPath(webpath);
File temp = new File(realPath);
// 文件夹check
if (!temp.getParentFile().exists() && !temp.getParentFile().isDirectory()) {
temp.getParentFile().mkdirs();
}
try {
// 成功转存数据文件
file.transferTo(temp);
map.put("success", "上传成功");
} catch (Exception e) {
map.put("fail", "上传失败:" + e.getMessage());
e.printStackTrace();
}
map.put("webpath", webpath);
temp = null;
return map;
} /***
* 下载
*
*/
public String downLoad(HttpServletRequest request,HttpServletResponse response,String path){
String realPath = request.getServletContext().getRealPath(path);
File file = new File(realPath);
if(file.exists()){ //判断文件父目录是否存在
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition","attachment;fileName=" + file.getName());// 设置文件名
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return null;
} public Map<String, String> uploadFilePic(HttpServletRequest request,MultipartFile file,String path) {
// 检查文件是否上传过 上传成功文件名规则:用户单位_上传文件名称
Map<String, String> map= new HashMap<String, String>();
String realPath = request.getServletContext().getRealPath(path);
File temp = new File(realPath);
// 文件夹check
if (!temp.getParentFile().exists() && !temp.getParentFile().isDirectory()) {
temp.getParentFile().mkdirs();
}
try {
// 成功转存数据文件
file.transferTo(temp);
map.put("success", "上传成功");
} catch (Exception e) {
map.put("fail", "上传失败:" + e.getMessage());
e.printStackTrace();
}
map.put("webpath", path);
temp = null;
return map;
}
}

import java.io.IOException;
import java.util.Map; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
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; @Controller
@RequestMapping("/upload")
public class FileUploadController {
private static Logger log = LogManager.getLogger(FileUploadController.class);
private static final String PathRoot = "SystemFile/upload/img";
@Resource
private FileService fileService; @RequestMapping("/save")
@ResponseBody
public Map<String, String> uploadFile(@RequestParam("file")MultipartFile file,@RequestParam("xxmc")String xxmc, HttpServletRequest request)
throws IllegalStateException, IOException {
log.info("===========进入文件上传方法===============");
if(!file.isEmpty())
{
Map<String, String> map=fileService.uploadFile(request,file,PathRoot+"/"+xxmc);
return map;
}
return null;
} }

文件上传-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. 01 学习数据分析的python库

    网页爬取 1.requests 2.BeautifulSoup 3.Scrapy 科学计算与数据分析 1.scipy 2.numpy 3.pandas 机器学习和深度学习 1.Scikit-learn ...

  2. Paper | Noise2Noise: Learning Image Restoration without Clean Data

    目录 故事背景 算法原理 点估计 神经网络算法与点估计的关系 核心思想 回头品味 实验 高斯 其他生成噪声 发表在2018 ICML. 摘要 We apply basic statistical re ...

  3. Paper | Quality assessment of deblocked images

    目录 1. 故事 2. 失真变化 3. 方法(PSNR-B) 4. 实验 这篇文章提出了一个PSNR-B指标,旨在衡量 压缩图像的块效应强度 或 去块效应后的残留块效应强度(比较去块效应算法的优劣). ...

  4. LeetCode 150:逆波兰表达式求值 Evaluate Reverse Polish Notation

    题目: 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. Evaluate the value of an arithm ...

  5. 简明了解apply()和call()

    apply()和call()都是ES6语法的,并且都是函数的方法. function foo() { alert(this.name) } var obj = { name: '小明' } foo() ...

  6. 【前端知识体系-JS相关】对移动端和Hybrid开发的理解?

    1.hybrid是什么,为何使用hybrid呢? 概念: hybrid就是前端和客户端的混合开发 需要前端开发人员和客户端开发人员配合完成 某些环节也可能会涉及到server端 大前端:网页.APP. ...

  7. 什么是JavaBean?

    什么是JavaBean? 首先明确的是JavaBean是一种Java类,而且是一种特殊的.可重用的类. 必须具有无参数的构造器,所有的属性都是private的,通过提供setter和getter方法来 ...

  8. jvm的组成入门

    JVM的组成分为整体组成部分和运行时数据区组成部分. JVM的整体组成 JVM的整体组成可以分为4个部分:类加载器(Classloader).运行时数据区(Runtime Data Area).执行引 ...

  9. vue+django分离开发的思想和跨域问题的解决

    一.介绍 在前后端分离的开发过程中,会涉及到跨域的问题,比如本次个人使用的Django+vue的框架,在vue对Django进行响应,Django再将响应的数据返回给vue,vue在进行渲染,如果不设 ...

  10. OpenGL入门1.3:着色器 GLSL

    前言 经过之前一段时间的学习(渲染管线简介)我们已经知道了着色器(Shader)是运行在GPU上的程序,这些小程序为图形渲染管线的某个特定部分而运行,着色器只是一种把输入转化为输出的程序,着色器也是一 ...