上传一张图片
//Controller代码
try {
MultipartHttpServletRequest request = (MultipartHttpServletRequest) this.getRequest();
MultipartFile shop_logo = request.getFile("file_shop_logo");
if (!shop_logo.isEmpty()) {
String img_shop_logo = WeiitUtil.uploadFile(shop_logo);
formMap.put("shop_logo",img_shop_logo);
}
}catch (Exception e){
e.printStackTrace();
throw new Exception("出错了");
}
//jsp代码
<div class="form-group">
<label class="control-label col-md-2 text-bold text-right">门店logo
</label>
<div class="col-sm-3">
<input type="file" name="file_shop_logo" class="bootstrap-uploader" id="file_shop_logo">
<br>
<span style="color: gray">建议尺寸:245 * 149 像素</span>
<input type="hidden" name="shop_logo" id="shop_logo" value="${imageDomain}${info.shop_logo}" class="hiddenClass" />
</div>
</div>
上传多张图片
//Controller代码
try {
MultipartHttpServletRequest request = (MultipartHttpServletRequest) this.getRequest();
String[] imgArr = this.getRequest().getParameterValues("imagesList");
if (imgArr!=null) {
List arrList= Arrays.asList(imgArr);
imagesList= new ArrayList(arrList);
}
Map<String, MultipartFile> fileMap = request.getFileMap();
if (fileMap!=null && !fileMap.isEmpty()) {
for (Iterator iterator=fileMap.keySet().iterator();iterator.hasNext();) {
String key = iterator.next().toString();
MultipartFile mFile = fileMap.get(key);
if (mFile != null) {
String img_url = WeiitUtil.uploadFile(mFile);
imagesList.add(mg_url);
}
}
}
formMap.put("imagesList", imagesList);
}catch (Exception e){
e.printStackTrace();
throw new Exception("出错了");
}
String product_img="";
if (imagesList != null && imagesList.size()>0) {
product_img=imagesList.get(0).toString();
}
formMap.put("product_img", product_img);
//jsp代码
<div class="form-group">
<label class="control-label col-xs-2 text-right">产品图片<span class="text-danger">*</span></label>
<div class="col-sm-9 control-label">
<div style="width: 600px; height: auto;" id="filediv">
<input id="Button1" class="btn btn-warning btn-xs" type="button"
value="选择图片"/>
<span style="color: gray"> 建议尺寸:600*600 像素。</span>
<div class="dropzone2" style="height:140px;" id="filedivUl">
</div>
</div>
</div>
</div>