You need to enable JavaScript to run this app. 如何在JSP中使用JavaScript、jQuery、Ajax上传多图到Spring Boot Controller
优惠活动
大模型
产品
解决方案
定价
更多

如何在JSP中使用JavaScript、jQuery、Ajax上传多图到Spring Boot Controller

前端多图上传+商品表单字段提交实现方案

第一步:修改JSP页面表单元素

给文件选择控件添加multiple属性支持多选图片,同时补充商品相关的表单字段:

<form id="productForm">
  <div>
    <label>商品名称:</label>
    <input type="text" name="productName" id="productName" required>
  </div>
  <div>
    <label>SKU编码:</label>
    <input type="text" name="skuCode" id="skuCode" required>
  </div>
  <div>
    <label>商品分类:</label>
    <select name="category" id="category" required>
      <option value="">请选择分类</option>
      <!-- 分类选项自行根据业务渲染 -->
    </select>
  </div>
  <div>
    <label>子分类:</label>
    <select name="subCategory" id="subCategory" required>
      <option value="">请选择子分类</option>
    </select>
  </div>
  <div>
    <label>标签:</label>
    <input type="text" name="tags" id="tags" placeholder="多个标签用逗号分隔">
  </div>
  <div>
    <label>商品图片:</label>
    <!-- 加multiple属性支持多选,accept限制仅选图片 -->
    <input type="file" id="file" name="files" multiple accept="image/*" required>
  </div>
  <button type="button" id="submitBtn">提交商品信息</button>
</form>
<!-- 存储接口返回的图片路径 -->
<input type="hidden" id="imagepath" name="imagePaths">

第二步:修改前端JS提交逻辑

原有逻辑仅支持单文件上传,调整为循环添加所有选中的图片到FormData,同时把商品其他字段也加入提交参数,注意参数名要和后端接收的files保持一致:

$("#submitBtn").click(function(){
  var formData = new FormData();
  var fileSelect = document.getElementById("file");
  // 校验是否选中文件
  if(!fileSelect.files || fileSelect.files.length == 0) {
    $("#file").focus();
    alert("请至少选择一张商品图片");
    return false;
  }
  // 循环添加所有选中的图片,用append方法才能生成数组参数
  for(let i=0; i<fileSelect.files.length; i++){
    let file = fileSelect.files[i];
    formData.append("files", file, file.name);
  }
  // 添加商品其他字段到提交参数
  formData.append("productName", $("#productName").val().trim());
  formData.append("skuCode", $("#skuCode").val().trim());
  formData.append("category", $("#category").val());
  formData.append("subCategory", $("#subCategory").val());
  formData.append("tags", $("#tags").val().trim());

  var request = new XMLHttpRequest();
  try {
    request.onreadystatechange=function() {
      if(request.readyState==4) {
        var v = JSON.parse(request.responseText);
        if(v.status==="OK") {
          alert("商品信息提交成功,图片上传完成");
          document.getElementById('imagepath').value = v.response;
          // 业务后续逻辑自行补充,比如重置表单、跳转列表页
        } else {
          alert("提交失败:" + (v.responseMessage || "未知错误"));
        }
      }
    }
    request.open('POST',"<%=AkApiUrl.testuploadfile%>");
    // 不要手动设置Content-Type,浏览器会自动生成带boundary的multipart/form-data请求头
    request.send(formData);
  } catch(e) {
    swal("无法连接到服务器","","error");
  }
});

第三步:后端Controller补充字段接收(可选)

如果需要在同一个接口同时接收图片和商品字段,在方法参数中添加对应参数即可:

@RequestMapping(value = AkApiUrl.testuploadfile, method = { RequestMethod.POST }, produces = {MediaType.APPLICATION_JSON_VALUE }) 
public ResponseEntity<?> testuploadfile(HttpServletRequest request, 
                                        @RequestParam("files") MultipartFile[] files,
                                        @RequestParam("productName") String productName,
                                        @RequestParam("skuCode") String skuCode,
                                        @RequestParam("category") String category,
                                        @RequestParam("subCategory") String subCategory,
                                        @RequestParam(value = "tags", required = false) String tags) {
  // 原有图片上传逻辑保持不变,新增字段可直接获取存入业务库
  // ......原有逻辑
}

注意事项

  • 多文件提交必须用append方法添加同名的files参数,不能用set方法,否则会覆盖参数仅保留最后一个文件
  • 如果需要实现选中图片后预览的功能,可以在文件选择的change事件里遍历文件列表,通过FileReader生成临时url渲染到img标签即可

内容的提问来源于stack exchange,提问作者Rakhi Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 13:36:00