layui的多图片传输,一次链接实现---Ajax与SSH之间FormData()的传递

前端js

需要导入jquery.js 下载

<script type="text/javascript">
$("#testListAction").click(function () {
            var form = new FormData();
            for(let i in file){
                form.append("file",file[i]);//表单文件流
            }

          $.ajax({
              url: '<%=path%>/biz/UploadImage_mUploadImage.action' //上传接口
              ,type: "POST"
              ,data: form
              ,contentType: false
              ,processData: false
              ,success: function (data) {
                   console.log(data)
              }
              ,error: function () {
                  alert(0)

              }
          })
        })
        </script>

Action中的代码

   protected static final String MAIN_JSP = "/WEB-INF/page/main.jsp";
    protected static final String UP_IMAGE_JSP = "/WEB-INF/page/Upload_Image.jsp";
    private JSONArray data;
    private UploadImage uploadImage = new UploadImage();

    private HttpServletRequest request = ServletActionContext.getRequest();
    //请求过来的路径
    private String realURL = request.getSession().getServletContext().getRealPath("/");
    //文件存储路径
    private final String TEMP_URL = realURL; 
    public JSONArray getData() {
        return data;
    }

    public void setData(JSONArray data) {
        this.data = data;
    }
    public File[] getFile() {
        return file;
    }

    public void setFile(File[] file) {
        this.file = file;
    }

    private File[] file;//解析请求信息,获取表单传过来的File //获取FormData
    


    private String[] fileContentType;//上传图片类型

    public String[] getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String[] fileContentType) {
        this.fileContentType = fileContentType;
    }

    private String[] fileFileName;//文件名

    public String[] getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String[] fileFileName) {
        this.fileFileName = fileFileName;
    }
 //单链接多图片上传
    public String uploadImage() throws Exception {
        System.out.println("接收到前台发过来的信号");
        JSONArray jsonArray = new JSONArray();
        List<UploadImage> uploadImage = new ArrayList<UploadImage>();
        if(file.length>0){//有文件输入
        //调用图片上传的接口
        uploadImage = uploadImageService.uploadImage(file, TEMP_URL, fileFileName);
        }
        setForwardView(MAIN_JSP);
        //如果上传成功,则返回消息
        if(!uploadImage.isEmpty()){
        request.setAttribute("msg",1);
        jsonArray = JSONArray.fromObject(uploadImage);
        System.out.println("--获取到转换为json格式的内容:"+jsonArray.toString());
        this.setData(jsonArray);
        }
        return SUCCESS;
    }

Service类

Properties properties = new Properties();
    protected final int SIZE = 10;//一次最多上传10个图片
    //上传图片的Service方法
    @Override
    public List<UploadImage> uploadImage(File[] files, String TEMP_URL, String[] fileFileName) throws IOException {
        //定义图片上传数组
        List<UploadImage> uploadImages = new ArrayList<UploadImage>();
        String[] uploadFileURL = new String[SIZE];
        //获取配置文件的信息
        properties = PropertiesLoaderUtils.loadAllProperties("conf//imagePath.properties");
        //获取配置文件的imagesURL
        String URL = properties.getProperty("imagesURL");
        TEMP_URL.replaceAll("/","/\\");
        File file = new File(TEMP_URL + URL);//文件存储绝对路径
        if(!file.exists()){
            file.mkdir();//如果文件不存在则创建文件
        }else {
            System.out.println("创建目录" + "失败,目标目录已经存在");
        }
        boolean flag = false;
        for(int i=0;i<files.length;i++){
            //图片文件名   为避免重名生成一个uuid作为文件名的前缀
            String imageName = "image-" + UUID.randomUUID().toString().replace("-", "") + "_" + fileFileName[i];
            //把图片拷贝到对应的文件夹
            try {
                FileUtils.copyFile(files[i], new File(file, imageName));
                flag = true;
            }catch (Exception e){
                e.printStackTrace();
            }
            if(true==flag){//图片上传成功
                //文件相对路径
                uploadFileURL[i] = URL.replaceAll("\\\\", "") + "/" + imageName;

                System.out.println("文件名:" + imageName);
                //文件类型
                String fileType = fileFileName[i];
                //获取文件后缀名
                fileType = fileType.substring(fileType.lastIndexOf(".") + 1);

                System.out.println("文件类型:" + fileType);
                //创建图片信息类并初始化参数
                UploadImage uploadImage = new UploadImage(getImageSize(files[i].length()), fileType, imageName, TEMP_URL.replaceAll("\\\\", "/") + uploadFileURL[i], uploadFileURL[i]);
                //存储图片文件信息到数组中
                uploadImages.add(uploadImage);
            }
        }

        return uploadImages;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,这里是一个简单的示例,二次封装了 `el-form` 组件,实现了表单验证和表单重置的功能: ```vue <template> <my-form :form-data="formData" :form-rules="formRules" @submit="handleSubmit" @reset="handleReset" /> </template> <script> import MyForm from '@/components/MyForm' export default { components: { MyForm }, data() { return { formData: { username: '', password: '' }, formRules: { username: [{ required: true, message: '请输入用户名', trigger: 'blur' }], password: [{ required: true, message: '请输入密码', trigger: 'blur' }] } } }, methods: { handleSubmit(formData) { // 表单提交处理逻辑 console.log(formData) }, handleReset() { // 表单重置处理逻辑 console.log('表单已重置') } } } </script> ``` `MyForm` 组件的代码如下: ```vue <template> <el-form :model="formData" :rules="formRules" ref="form" label-width="100px"> <slot></slot> <el-form-item> <el-button type="primary" @click="submitForm">提交</el-button> <el-button @click="resetForm">重置</el-button> </el-form-item> </el-form> </template> <script> export default { props: { formData: Object, formRules: Object }, methods: { submitForm() { this.$refs.form.validate(valid => { if (valid) { // 表单验证通过,提交表单数据 this.$emit('submit', this.formData) } else { // 表单验证失败 console.log('表单验证失败') return false } }) }, resetForm() { this.$refs.form.resetFields() this.$emit('reset') } } } </script> ``` 在 `MyForm` 组件中,使用 `slot` 插槽来实现动态插入表单项,使用 `props` 属性来接收父组件传递的表单数据和表单验证规则,使用 `$emit` 方法来向父组件发送表单提交和表单重置的事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HHUFU..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值