mini-fileupload实现图片上传

页面

<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/
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值