上传单个文件
html:
Js:
var xa = new FormData($("#testForm")[0]);
$.ajax({
type: "POST",//请求得POST
url: ajaxUrl + "cms/from_File",//自己的路径
data: xa,
dataType: "text",
async: false,
cache: false,
contentType: false,
processData: false,
success: function(data) {
//。。。。。。。。。。。
},
error: function(e) {
layer.alert('网络异常');
}
});
return false;
});
Java:
@RequestMapping(value = "/from_File", method = RequestMethod.POST)
public String layout_edit_update(@RequestParam("imgFile") MultipartFile imgFile, HttpServletRequest request) throws FileNotFoundException, ParseException {
String uName= request.getParameter("uName");
String DirPath = "D:\\test\\";
File outDir = new File(DirPath);
outDir.mkdirs();//校验文件夹路径是否存在,不存在创建
String FilePath = DirPath +"111.png";//文件路径格式视情况
getFile(imgFile,FilePath);
return String.valueOf(1);
}
public static boolean getFile(MultipartFile file, String outPath) {
try {
File newFile = new File(outPath);
FileOutputStream Os = new FileOutputStream(newFile);
BufferedOutputStream out = new BufferedOutputStream(Os);
out.write(file.getBytes());
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
Java 配置文件application.properties 设置文件大小:
#上传文件大小
# Single file max size
multipart.maxFileSize=50Mb
# All files max size
multipart.maxRequestSize=50Mb
启动类配置文件上传大小:
import javax.servlet.MultipartConfigElement;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
@Configuration
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
System.out.println( "SpringBoot Start Success!" );
}
/**
* 文件上传配置
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个文件最大
factory.setMaxFileSize("10240KB"); //KB,MB
/// 设置总上传数据总大小
factory.setMaxRequestSize("102400KB");
return factory.createMultipartConfig();
}
==========================================================================================
上传多文件:
html:
Js:
var xa = new FormData($("#testForm")[0]);
$.ajax({
type: "POST",//请求得POST
url: ajaxUrl + "cms/from_File",//自己的路径
data: xa,
dataType: "text",
async: false,
cache: false,
contentType: false,
processData: false,
success: function(data) {
//。。。。。。。。。。。
},
error: function(e) {
layer.alert('网络异常');
}
});
return false;
});
Java:
@RequestMapping(value = "/from_File", method = RequestMethod.POST)
public String layout_edit_update(@RequestParam("Files") MultipartFile Files, HttpServletRequest request) throws FileNotFoundException, ParseException {
String uName= request.getParameter("uName");
String DirPath = "D:\\test\\";
File outDir = new File(DirPath);
outDir.mkdirs();//校验文件夹路径是否存在,不存在创建
if (Files!= null && Files.length > 0) {
for (int i = 0; i < Files.length; i++) {
MultipartFile file = Files[i];
String FilePath = DirPath +i+".png";//文件路径格式视情况
getFile(imgFile,FilePath);//输出保存文件
}
}
return String.valueOf(1);
}
public static boolean getFile(MultipartFile file, String outPath) {
try {
File newFile = new File(outPath);
FileOutputStream Os = new FileOutputStream(newFile);
BufferedOutputStream out = new BufferedOutputStream(Os);
out.write(file.getBytes());
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}