首页 > 学院 > 开发设计 > 正文

多图片上传

2019-11-09 16:44:49
字体:
来源:转载
供稿:网友

1.div部分

		                       <div>						<span>添加图片:</span> 						<span id="attrFieldUploader"							style="width: 150px;">							<div id="fileUploader" style="width:102px; height: 34px; display:inline" >								<input name="file[]" type="file" accept="image/jpg,image/png" style="width: 70px" multiple/>								<div class="easyui-PRogressbar" style="width: 170px"></div>							</div>						</span>     						<span id="preview" style="display: none">						<!-- 	<button onclick='c$.onLayout();return false;'>预览</button>  							<button onclick='c$.onClearImage();return false;'>清除</button> -->						</span>					</div>	2.后台处理

	@ResponseBody	@RequestMapping(value = "uploadx")  	public ActionResult uploadx(@RequestParam("cate") String category, @RequestParam("file[]") MultipartFile file, HttpServletRequest request, ModelMap model) {  		  		//保存  		try { 			String fileName = this.fileService.getFilePath(category, file.getOriginalFilename());			String path = this.fileService.resolveToPhysicalFileName(fileName);			File targetFile = new File(path);  			if(!targetFile.exists()){  				targetFile.mkdirs();  			}  			file.transferTo(targetFile);			return ActionResult.Succeed(fileName);		} catch (Exception e) {  			logger.warn("", e);			return ActionResult.Failed(e);		}  			}3.service

package com.eznext.modul.file.service;import java.io.File;import java.math.BigDecimal;import java.sql.Date;import java.util.Map;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Service;import com.eznext.core.util.md5;import com.eznext.core.web.BusinessException;@Servicepublic class FileService {	 private static Logger logger = LoggerFactory				.getLogger(FileService.class);	 	 private Map<String, String> categories;	 	 	 public String getFilePath(String category, String filename) throws BusinessException{		int index = filename.lastIndexOf(".");		 if(index < 0){			 throw new BusinessException("无效的文件名!");		 }		 String cateId = String.format("%s.name", category.toLowerCase());		 String name = categories.get(cateId);		 if(name == null)		 {			 throw new BusinessException("无效的属性名!");		 }		 return name + "_" + uniqueName(filename.substring(0, index)) + "." + filename.substring(index + 1);	 }	 	 public String resolveToPhysicalFileName(String filename) throws BusinessException{		 int index = filename.indexOf("_");		 if(index < 0){			 throw new BusinessException("无效的文件名!");		 }		 String path = categories.get(String.format("%s.path", filename.subSequence(0, index)));		 if(path == null)		 {			 throw new BusinessException("无效的属性名!");		 }		 return path + "/" + filename.substring(index + 1);	 }	 	 public void deleteFile(String filename) throws BusinessException{		 	String physicalFilename = this.resolveToPhysicalFileName(filename);			File file = new File(physicalFilename);			if(file.exists() && file.isFile()){				file.delete();			}	 }	 	 public Map<String, String> getCategories() {		return categories;	}	public void setCategories(Map<String, String> categories) {		this.categories = categories;	}	private String uniqueName(String name){		BigDecimal vv = new BigDecimal(System.currentTimeMillis());				return MD5.getMd5String(name + vv.toPlainString());	 }}4.配置文件

<bean id="fileService" class="com.eznext.modul.file.service.FileService">		<property name="categories">			<map>			    <entry key="maintain.name" value="maintain"></entry>			    <entry key="maintain.path" value="${fileservice.baseDir}/maintain"></entry>			</map>		</property>	</bean> 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表