页面
<th style="width:50px;font-size: 10px; font-weight: normal">旧表图片:</th>
<td>
<input id="oldImg" type="file" class="mini-fileupload" name="oldImg" limitType="*.png;*.jpg;*.xls,*.dwg;*.vsd"
limitSize="5MB" flashUrl="/scripts/SWFupload/swfupload.swf"
uploadUrl="" onuploadsuccess="onUploadSuccess"
onuploaderror="onUploadError" onfileselect="onFileSelect"/>
<!--<a class="mini-button" id="importBtnOld" onclick="startUpload()">上传</a>-->
</td>
js
//动态设置url
var oldFileupload = mini.get("oldImg");
oldFileupload.setUploadUrl("/archives/changeMeter/upload");
function onFileSelect(e) {
}
function onUploadSuccess(e) {
alert(e.serverData);
this.setText("");
}
function onUploadError(e) {
alert(e.serverData)
}
function startUpload(changeMeterId) {
var oldFileupload = mini.get("oldImg");
oldFileupload.setPostParam({changeMeterId:changeMeterId});
oldFileupload.startUpload();
}
// 提交处理
$('#commitBtn').click(function () {
var changeMeterId = mini.get('changeMeterId').getValue();
startUpload(changeMeterId);
return;
commitChangeMeterProcess();
});
接口
@RequestMapping("/upload")
public String upload(
@RequestParam(value = "oldImg",required = false) MultipartFile oldImg,
@RequestParam(value = "changeMeterId",required = false)Integer changeMeterId){
if(oldImg.isEmpty()){
return "文件为空";
}
//获取文件名
String fileName = oldImg.getOriginalFilename();
//加时间戳
fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + "_" + fileName;
String path = "F:/img/" + fileName;
File dest = new File(path);
//文件是否存在
if (dest.exists()){
return "文件已经存在";
}
//文件父目录是否存在
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
oldImg.transferTo(dest);
String oldMeterImageUrl="/images/"+fileName;//本地运行项目
ChangeMeter changeMeter = new ChangeMeter();
changeMeter.setChangeMeterId(changeMeterId);
changeMeter.setOldMeterImageUrl(oldMeterImageUrl);
//保存到数据库
changeMeterService.insertUrl(changeMeter);
} catch (IOException e) {
e.printStackTrace();
return "上传失败";
}
return "上传成功";
}
package com.yunrun.swys.web.common.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import javax.servlet.MultipartConfigElement;
/**
* @ClassName WebAppConfig
* @Description:
* @Author zhangwei
* @Date 2019/10/24
* @Version V2.0
**/
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Value("${image.path.handler}")
private String handler;
@Value("${cbs.imagesPath}")
private String mImagesPath;
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory factory = new MultipartConfigFactory();
//文件最大KB,MB
factory.setMaxFileSize("5MB");
//设置总上传数据总大小
factory.setMaxRequestSize("20MB");
return factory.createMultipartConfig();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if(mImagesPath.equals("") || mImagesPath.equals("${cbs.imagesPath}")){
String imagesPath = WebAppConfig.class.getClassLoader().getResource("").getPath();
if(imagesPath.indexOf(".jar")>0){
imagesPath = imagesPath.substring(0, imagesPath.indexOf(".jar"));
}else if(imagesPath.indexOf("classes")>0){
imagesPath = "file:"+imagesPath.substring(0, imagesPath.indexOf("classes"));
}
imagesPath = imagesPath.substring(0, imagesPath.lastIndexOf("/"))+"/images/";
mImagesPath = imagesPath;
}
registry.addResourceHandler(handler).addResourceLocations(mImagesPath);
// TODO Auto-generated method stub
super.addResourceHandlers(registry);
}
}
配置路径
image.path.handler=/images/**
image.path.locations=file:D:/syys/images/
cbs.imagesPath=file:/F:/img/
来源:https://blog.csdn.net/sout_zx/article/details/102776101