页面
< 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
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;
@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 ( ) ;
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) ;
super . addResourceHandlers ( registry) ;
}
}
配置路径
image. path. handler= / images