首页 > 代码库 > spring mvc 图片上传,图片压缩、跨域解决、 按天生成目录 ,删除,限制为图片代码等相关配置

spring mvc 图片上传,图片压缩、跨域解决、 按天生成目录 ,删除,限制为图片代码等相关配置

spring mvc 图片上传,跨域解决 按天生成目录 ,删除,限制为图片代码,等相关配置

fs.root=data/#fs.root=/home/dev/fs/#fs.root=D:/fs/#fs.domains=182=http://172.16.100.182:18080,localhost=http://localhost:8080


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-lazy-init="false" default-autowire="no">

<!-- 图片上传配制 -->
<bean id="fileUpload" class="com.xyt.gsm.utils.FileUpload"> 
 <!-- 是否启用temp文件目录  -->
 <property name="UploadTemp" value=http://www.mamicode.com/"false"/>  >
package com.xyt.gsm.utils;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.xyt.gsm.constant.Fs;
import com.xyt.gsm.constant.Fs.UpoladStatus;

/**
 * 图片上传
 * 
 * @author liangrui
 * 
 *         UploadTemp 如果为false 临时所在目录被实际目录替换,如果为true 需要再做一次拷贝
 * 
 */
public class FileUpload
{

	// 文件信息
	/** 是否启用临时文件目录 */
	private static boolean UploadTemp = false;
	/** 文件上传相对 路径 */
	private static String RELATIVEPATH = "relativePath";
	/** 文件上传绝对 路径目录 */
	private static String ABSOLUTEPATH = "absolutePath";
	/** 实际文件所在目录 */
	private static String realFilePath = "fs/";
	/** 文件上传临时所在目录 */
	public static String UPLOADDIR = "fs/temp/";
	/** 图片上传大小限制 */
	public static Integer photoSize = 2097152;//
	/** 末知类型文件上大小限制 */
	private static Integer otherSize = 100000000;
	/** 是否存放相对路径 **/
	private static boolean RELATIVE_SAVE = false;

	// fied
	public void setUploadTemp(boolean uploadTemp)
	{
		UploadTemp = uploadTemp;
	}

	public void setPhotoSize(Integer photoSize)
	{
		FileUpload.photoSize = photoSize;
	}

	public void setOtherSize(Integer otherSize)
	{
		FileUpload.otherSize = otherSize;
	}

	public void setRealFilePath(String realFilePath)
	{
		if (UploadTemp == false)
			UPLOADDIR = realFilePath;
		FileUpload.realFilePath = realFilePath;
	}

	public void setUPLOADDIR(String uPLOADDIR)
	{
		if (UploadTemp == false)
			UPLOADDIR = realFilePath;
		else
			UPLOADDIR = uPLOADDIR;

	}

	public static void setRELATIVE_SAVE(boolean rELATIVE_SAVE)
	{
		RELATIVE_SAVE = rELATIVE_SAVE;
	}

	

	public static String getRealFilePath() {
		return realFilePath;
	}

	/**
	 * 
	 * @param request
	 *            上传文件的请求
	 * @param suffuxDir
	 *            目录分配
	 * @param type
	 *            上传文件的类型
	 * @param cutPhoto
	 *            是否切图
	 * @return 上传后的文件路径
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	// ===============================================================================================
	// -----解析request 上传 返回上传后的文件把在路径
	// ================================================================================================

	public static List<String> fileUpload(HttpServletRequest request, String suffuxDir, String type) throws IllegalStateException, IOException
	{

		return fileUpload(request, suffuxDir, type, false);
	}

	public static List<String> fileUpload(HttpServletRequest request, String suffuxDir, String type, Boolean cutPhoto) throws IllegalStateException, IOException
	{
		// 上传文件的解析器
		CommonsMultipartResolver mutiparRe = new CommonsMultipartResolver();
		if (mutiparRe.isMultipart(request))
		{// 如果是文件类型的请求

			List<String> fileInfo = new ArrayList<String>();
			MultipartHttpServletRequest mhr = (MultipartHttpServletRequest) request;

			// 按月分目录
			DateFormat df = new SimpleDateFormat("yyyyMM");
			String month = df.format(new Date());

			// 获取路径
			String uploadDir = "";
			if (RELATIVE_SAVE)
			{
				uploadDir += (request.getSession().getServletContext().getRealPath("/")+FileUpload.realFilePath);
			}else
			{
				uploadDir += FileUpload.UPLOADDIR ;
			}

			uploadDir+=(suffuxDir + month + "/");
			
			// 如果目录不存在,创建一个目录
			if (!new File(uploadDir).exists())
			{
				File dir = new File(uploadDir);
				dir.mkdirs();
			}

			// 跌带文件名称
			Iterator<String> it = mhr.getFileNames();
			while (it.hasNext())
			{
				final MultipartFile mf = mhr.getFile(it.next()); // 获取一个文件
				if (mf != null)
				{
					// 如果是图片类型文件上传 限制大小和类型
					if (type.equals(Fs.FileUpLoadType.PHOTO))
					{
						if (!checkingPhoto(mf))
						{
							fileInfo.add(UpoladStatus.类型无效或图片过大.toString());
							continue;
						}
					} else
					{
						if (mf.getSize() > otherSize)
						{
							fileInfo.add(UpoladStatus.类型无效或图片过大.toString());
							continue;
						}
					}
					// 其它上传类型 另外做限制判断

					String resFileName = mf.getOriginalFilename();// 获取原文件名
					// 取得文件名和后辍
					String suffix = "";
					if (resFileName.indexOf(".") != -1)
					{
						suffix = resFileName.substring(resFileName.lastIndexOf(".") + 1);
					}

					String dian = ".";
					if ("".equals(suffix))
						dian = "";

					// 保存的文件名
					String uuid = rename();
					String fileName = uuid + dian + suffix;
					File outFile = new File(uploadDir + fileName); // 路径加文件名 +"/"
					mf.transferTo(outFile); // 保存到

					if (cutPhoto)
					{
						final String dirAndUuid = uploadDir + uuid;
						final String dians = dian;
						final String suffixs = suffix;

						new Thread(new Runnable()
						{
							public void run()
							{
								try
								{
									copyImgSpecification(mf.getInputStream(), dirAndUuid, dians, suffixs);
								} catch (IOException e)
								{
									e.printStackTrace();
								}
							}
						}).start();
					}

					if (RELATIVE_SAVE)
					{// 相对路径目录
						fileInfo.add("/" + FileUpload.realFilePath + suffuxDir + month + "/" + fileName);// 文件路径
					} else
					{// 绝对路径目录
						fileInfo.add("/" + suffuxDir + month + "/" + fileName);// 文件路径
					}
				}
			}
			return fileInfo;
		}
		return null;
	}

	// ------------------------------------------------------------------------------------------------------------
	// 复制图片 改变不同大小的规格
	// ------------------------------------------------------------------------------------------------------------
	public static void copyImgSpecification(InputStream is, String DirAnduuid, String dian, String suffix) throws IOException
	{

		Image img = ImageIO.read(is);
		FileOutputStream fos = null;
		for (int i = 0; i < Fs.FileUpLoadType.changePhotoSize.length; i++)
		{
			int w = Fs.FileUpLoadType.changePhotoSize[i];
			// int h=imgSize[i];
			String allPath = DirAnduuid + "_" + w + "x" + w + dian + suffix;
			if ("".equals(suffix))
			{
				suffix = "jpg";
			}

			fos = new FileOutputStream(allPath); // 输出到文件流
			chagePhotoSize(allPath, img, fos, suffix, w, w);
		}

		img.flush();
		// if(fos!=null){fos.close();}
	}

	// ----------------------------------------------------------------
	// 检查图片类型上传和图片大小
	// ----------------------------------------------------------------
	public static boolean checkingPhoto(MultipartFile mf) throws IOException
	{
		if (!isImage(mf.getInputStream()))
		{ // 判断是否为图片
			// fileInfo.add("请上传有效图片文件");//提示信息
			return false;
		}
		if (mf.getSize() > photoSize)
		{
			// fileInfo.add("图片大于"+(new
			// DecimalFormat("####.##").format(((double)photoSize)/1024/1024))+"MB上传失败");//提示信息
			return false;
		}
		return true;
	}

	// 只生成uuid 和后辍分开,切图时需要
	public static String rename(/* String name */)
	{
		// 时分秒
		/*
		 * Long now = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss") .format(new Date()));
		 * Long random = (long) (Math.random() * now); String fileName = now + "" + random;
		 */
		// 不能设置种子值,应采用默认的方式(系统时间戳 )
		final Random rd = new Random();
		final Long random = rd.nextLong();
		String num = String.valueOf(random);
		// 限制5个字符
		int limit = 5;
		if (num.length() > limit)
		{
			num = num.substring(0, limit);
		}
		// uuid生成文件名
		UUID uuid = UUID.randomUUID();
		// 加上 5位随机数
		String uustr = uuid.toString().replace("-", "") + num;

		/*
		 * if (name.indexOf(".") != -1) { uustr += name.substring(name.lastIndexOf(".")); } else {
		 * return uustr; }
		 */

		return uustr;
	}

	// 更改文件名称
	public static String rename(String name)
	{
		// 时分秒
		/*
		 * Long now = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss") .format(new Date()));
		 * Long random = (long) (Math.random() * now); String fileName = now + "" + random;
		 */
		// 不能设置种子值,应采用默认的方式(系统时间戳 )
		final Random rd = new Random();
		final Long random = rd.nextLong();
		String num = String.valueOf(random);
		// 限制5个字符
		int limit = 5;
		if (num.length() > limit)
		{
			num = num.substring(0, limit);
		}
		// uuid生成文件名
		UUID uuid = UUID.randomUUID();
		// 加上 5位随机数
		String uustr = uuid.toString().replace("-", "") + num;

		if (name.indexOf(".") != -1)
		{
			uustr += name.substring(name.lastIndexOf("."));
		} else
		{
			return uustr;
		}

		return uustr;
	}

	// ===============================================================================================
	// -----判断是否为图片
	// ================================================================================================
	public static boolean isImage(File imageFile)
	{
		if (!imageFile.exists())
		{
			return false;
		}
		Image img = null;
		try
		{
			img = ImageIO.read(imageFile);
			if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0)
			{
				return false;
			}
			return true;
		} catch (Exception e)
		{
			return false;
		} finally
		{
			img = null;
		}
	}

	public static boolean isImage(InputStream imageFile)
	{
		Image img = null;
		try
		{
			img = ImageIO.read(imageFile);
			if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0)
			{
				return false;
			}
			return true;
		} catch (Exception e)
		{
			return false;
		} finally
		{
			img = null;
		}
	}

	// ===============================================================================================
	// -----删除
	// ================================================================================================
	public static int deleteFile(HttpServletRequest request, boolean cutPhoto, String... path) throws MalformedURLException, SmbException
	{
		// List<String> delList=new ArrayList<String>();//记录成功删除的文件名
		int delcount = 0;
		// 远程删除
		if (realFilePath.length() > 3 && realFilePath.substring(0, 3).equals("smb"))
		{
			for (String pa : path)
			{
				SmbFile remoteFile = new SmbFile(pa);
				if (remoteFile.exists())
				{
					remoteFile.delete();
					delcount++;
					// 获取文件名
					// delList.add(p.substring(p.lastIndexOf("/")+1));
				}
			}
		} else
		{

			// 获取当前根目录 本地删除
			String proPath = "";
			if (RELATIVE_SAVE)
			{
				proPath += request.getSession().getServletContext().getRealPath("/");
			} else
			{
				proPath += FileUpload.UPLOADDIR;
			}

			for (String p : path)
			{
				File file = new File(proPath + p);
				if (file.exists())
				{
					file.delete();
					delcount++;
					// 获取文件名
					// delList.add(p.substring(p.lastIndexOf("/")+1));
				
				// 删除小图
				if (cutPhoto)
				{
					Integer[] xPhotoArr = Fs.FileUpLoadType.changePhotoSize;
					for (Integer imgSize : xPhotoArr)
					{
						String suffixs ="";
						String prefix=p;
						
						if(p.indexOf(".")!=-1){
						 suffixs = p.substring(p.lastIndexOf("."));
						 prefix = p.substring(0, p.lastIndexOf("."));
						}
						String newP = prefix + "_" + imgSize + "x" + imgSize + suffixs;
						File filex = new File(proPath + newP);
						if (filex.exists())
						{
							filex.delete();
							delcount++;
						}

					}
				}
				}
			}
		}
		return delcount;
	}

	/**
	 * 
	 * @param request
	 * @param path
	 *            临时图片目录
	 * @param prefixDir
	 *            前辍目录 比如 "商品ID目录,商品图片目录"
	 * @return
	 * @throws IOException
	 */
	// -------------------------------------------------------------------------------------------
	// 把temp的图片文件拷贝到 真实存放文件目录/日期文件夹中
	// -----------------------------------------------------------------------------------------------
	public static String tempToupdateImgPath(HttpServletRequest request, String path, String... prefixDir) throws IOException
	{
		// 主目录
		String rootDir = request.getSession().getServletContext().getRealPath("/");
		File file = new File(rootDir + path);// 需要移动的图片

		String realPath = "";// 实际图片地址
		String dbPath = "";// 保存数据库
		String fileName = path.substring(path.lastIndexOf("/") + 1);// 文件名
		// 加上前辍目录
		StringBuffer appendDir = new StringBuffer();
		for (String zdir : prefixDir)
		{
			appendDir.append(zdir + "/");
		}

		// smb://username:password@192.168.0.77
		// 如果是smb远程 上传远程服务器
		if (realFilePath.length() > 3 && realFilePath.substring(0, 3).equals("smb"))
		{
			dbPath = fileToRemoto(file, fileName, appendDir.toString());
		} else
		{
			// 上传到本地服务器
			dbPath = realFilePath + appendDir.toString() + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/";

			realPath = rootDir + dbPath;
			if (!new File(realPath).exists())
			{
				File dir = new File(realPath);
				dir.mkdirs();
			}

			file.renameTo(new File(realPath + fileName));
		}

		return "/" + dbPath + fileName;
	}

	// -------------------------------------------------------------------------------------------
	// 通用smb 传送文件到远程 配制路径 前三个字符为 smb开头
	// -----------------------------------------------------------------------------------------------
	private static String fileToRemoto(File file, String fileName, String prefixDir)
	{
		InputStream in = null;
		OutputStream out = null;
		String returnPath = null;
		try
		{
			// 获取流
			in = new BufferedInputStream(new FileInputStream(file));
			String dateDir = new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/";
			SmbFile remoteFile = new SmbFile(realFilePath + dateDir);
			// ip+...
			String fixed = realFilePath.substring(realFilePath.indexOf('@') + 1);
			returnPath = fixed + prefixDir + dateDir + fileName;

			if (!remoteFile.exists())
			{
				remoteFile.mkdirs();

			}

			// 将远程的文件路径传入SmbFileOutputStream中 并用 缓冲流套接
			String inputFilePath = remoteFile + "/" + fileName;

			// System.out.println("remoteFile:  "+remoteFile);
			out = new BufferedOutputStream(new SmbFileOutputStream(inputFilePath));
			byte[] buffer = new byte[1024];
			while (in.read(buffer) != -1)
			{
				out.write(buffer);
				buffer = new byte[1024];
			}

		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (in != null)
				{
					in.close();
				}
				if (out != null)
				{
					out.close();
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}

		return returnPath;
	}

	// ----------------------------------------------------------------------------------------
	// 改变图片大小
	// --------------------------------------------------------------------------------------
	private static void chagePhotoSize(final String path, Image src/* FileInputStream is */, FileOutputStream fos, final String format, int w, int h) throws IOException
	{

		try
		{
			int old_w = src.getWidth(null); // 得到源图宽
			int old_h = src.getHeight(null);
			int new_w = 0;
			int new_h = 0; // 得到源图长
			double w2 = (old_w * 1.00) / (w * 1.00);
			double h2 = (old_h * 1.00) / (h * 1.00);
			/**
			 * BufferedImage(,,) width - 所创建图像的宽度 height - 所创建图像的高度 imageType - 所创建图像的类型
			 */
			// 图片跟据长宽留白,成一个正方形图。
			BufferedImage oldpic;
			if (old_w > old_h)
			{
				// BufferedImage.TYPE_INT_RGB; //0-13
				oldpic = new BufferedImage(old_w, old_w, 1);
			} else
			{
				if (old_w < old_h)
				{
					oldpic = new BufferedImage(old_h, old_h, 1);
				} else
				{
					oldpic = new BufferedImage(old_w, old_h, 1);
				}
			}
			// 改变图片大小
			Graphics2D g = oldpic.createGraphics();
			g.setColor(Color.white);
			if (old_w > old_h)
			{
				/**
				 * x - 要填充矩形的 x 坐标。 y - 要填充矩形的 y 坐标。 width - 要填充矩形的宽度。 height - 要填充矩形的高度。
				 */
				g.fillRect(0, 0, old_w, old_w);

				/**
				 * img - 要绘制的指定图像。如果 img 为 null,则此方法不执行任何操作。 x - x 坐标。 y - y 坐标。 width - 矩形的宽度。
				 * height - 矩形的高度。 bgcolor - 在图像非透明部分下绘制的背景色。 observer - 当转换了更多图像时要通知的对象。
				 */
				g.drawImage(src, 0, (old_w - old_h) / 2, old_w, old_h, Color.white, null);
			} else
			{
				if (old_w < old_h)
				{
					g.fillRect(0, 0, old_h, old_h);
					g.drawImage(src, (old_h - old_w) / 2, 0, old_w, old_h, Color.white, null);

				} else
				{
					// g.fillRect(0,0,old_h,old_h);
					/**
					 * getScaledInstance() width - 将图像缩放到的宽度。 height - 将图像缩放到的高度。 hints -
					 * 指示用于图像重新取样的算法类型的标志。
					 */

					g.drawImage(src.getScaledInstance(old_w, old_h, Image.SCALE_SMOOTH), 0, 0, null);
				}
			}

			g.dispose();
			src = http://www.mamicode.com/oldpic;>
package com.xyt.gsm.service.fs;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.xyt.gsm.bean.common.ResultBean;
import com.xyt.gsm.entity.mgr.User;

public interface FileUploadService
{

	String singleUpload(HttpServletRequest request, User user, String suffixDir, String type, Boolean cutPhoto) throws Exception;//

	List<?> MonyUpload(HttpServletRequest request, User user, String suffixDir);//

	int deleteFile(HttpServletRequest request, boolean cutPhoto, String... path);

	ResultBean deleteFileAnDb(HttpServletRequest request, String type, String id, boolean cutPhoto, String... path) throws Exception;

	String singleUpload(HttpServletRequest request, User user, String suffixDir, String type, Boolean cutPhoto, String merchantId) throws Exception;//

	List<String> singleUpload(HttpServletRequest request, String suffixDir, String type, Boolean cutPhoto, String merchantId) throws Exception;//

	void download();

}

package com.xyt.gsm.service.fs.imp;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xyt.gsm.bean.common.ResultBean;
import com.xyt.gsm.constant.CommonConstants;
import com.xyt.gsm.constant.DictConstants;
import com.xyt.gsm.constant.Fs;
import com.xyt.gsm.constant.Fs.UpoladStatus;
import com.xyt.gsm.entity.mgr.User;
import com.xyt.gsm.service.fs.FileUploadService;
import com.xyt.gsm.service.mgr.DictService;
import com.xyt.gsm.service.mgr.ProductPhotoService;
import com.xyt.gsm.utils.FileUpload;

public class FileUploadServiceImp implements FileUploadService
{
	protected Log log = LogFactory.getLog(this.getClass().getName());

	private ProductPhotoService productPhotoService;
	private DictService dictService;

	public void setProductPhotoService(ProductPhotoService productPhotoService)
	{
		this.productPhotoService = productPhotoService;
	}

	public void setDictService(DictService dictService)
	{
		this.dictService = dictService;
	}

	@Override
	public String singleUpload(HttpServletRequest request, User user, String suffixDir, String type, Boolean cutPhoto, String merchantId) throws Exception
	{

		String path = "";
		// 图片上传类型
		if (type.equals(Fs.FileUpLoadType.PHOTO))
		{

			if (user == null)
				return UpoladStatus.上传失败末知错误.toString();

			if ("".equals(merchantId) || merchantId == null)
			{
				// 运营人员操作 为 另一目录下
				if (CommonConstants.RoleType.OPERATER.equals(user.getUserType()))
				{
					//merchantId = Fs.FileDir.OPERATION_DIR;
					merchantId = "";
				} else
				{
					//获取供应商id
					merchantId = user.getMerchantId();
				}
			}

			// 判断数据字典里是否有该目录
			Map<String, String> photos = (Map<String, String>) dictService.getDictMap(DictConstants.ClsCode.FS);
			if (!matchDir(photos, suffixDir))
			{
				// suffixDir=FileDir.NOT_DIR;
				path=com.xyt.gsm.constant.Fs.UpoladStatus.文件目录未分配.name();

			} else
			{

				// 商家id /处理
				merchantId = manageSeparator(merchantId);
				// suffixdri
				suffixDir = manageSeparator(suffixDir);

				path = FileUpload.fileUpload(request, merchantId + suffixDir, Fs.FileUpLoadType.PHOTO, cutPhoto).get(0);

			}
		}

		return path;
	}

	@Override
	public String singleUpload(HttpServletRequest request, User user, String suffixDir, String type, Boolean cutPhoto) throws Exception
	{

		return singleUpload(request, user, suffixDir, type, cutPhoto, "");
	}

	@Override
	public List<?> MonyUpload(HttpServletRequest request, User user, String suffixDir)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteFile(HttpServletRequest request, boolean cutPhoto, String... path)
	{

		return 0;
	}

	@Override
	public void download()
	{
		// TODO Auto-generated method stub

	}

	// 处理 / 前后有无/都可以
	public String manageSeparator(String str)
	{
		if (str == null)
			str = "";
		if (!"".equals(str))
		{// --05/01/
			// 不需要/
			if (str.startsWith("/"))
				str = str.substring(1);
			// 要加上/
			if (!str.endsWith("/"))
				str = str + "/";
		}

		return str;
	}

	// -------------------------------------------
	// 匹配前端的目录和数据库的目录是否一至,以防前后有无/问题
	// -------------------------------------------
	public boolean matchDir(Map<String, String> photos, String webDir)
	{
		if (photos == null)
			return false;
		if (webDir == null)
			webDir = "";

		boolean isMatch = false;

		if (photos.containsKey(webDir))
			return true;
		String temp = "";
		// 不匹配的情况下!再进行前后/处理匹配
		if (webDir.startsWith("/"))
		{
			// 前面 不要/
			temp = webDir.substring(1);
			if (photos.containsKey(temp))
				return true;

		}

		if (webDir.endsWith("/"))
		{
			// 后面 不要/
			temp = webDir.substring(0, webDir.length() - 1);
			if (photos.containsKey(temp))
				return true;
		}

		if (!webDir.endsWith("/"))
		{
			// 后面 要/
			temp = webDir + "/";
			if (photos.containsKey(temp))
				return true;
		}

		if (!webDir.startsWith("/"))
		{
			// 前面 要/
			temp = "/" + webDir;
			if (photos.containsKey(temp))
				return true;
		}

		// 前面 后面要/
		if (!webDir.startsWith("/") && !webDir.endsWith("/"))
		{
			temp = "/" + webDir + "/";
			if (photos.containsKey(temp))
				return true;
		}

		// 前面 后面 不要/
		if (webDir.startsWith("/") && webDir.endsWith("/"))
		{
			temp = webDir.substring(1, webDir.length() - 1);
			if (photos.containsKey(temp))
				return true;
		}

		return isMatch;
	}

	@Override
	public ResultBean deleteFileAnDb(HttpServletRequest request, String type, String id, boolean cutPhoto, String... path) throws Exception
	{

		ResultBean rb = new ResultBean();

		if (type.equals(Fs.FileUpLoadType.PHOTO))
		{
			// 删除数据
			int count = productPhotoService.deleteByProductId(id);
			if (count > 0)
			{
				// 删除图片
				int cou = FileUpload.deleteFile(request, cutPhoto, path);
				if (0 >= cou)
				{
					log.warn("id:" + id + ": 数据删除成功!图片删除失败");
				}

			} else
			{
				rb.setSuccess(false);
			}
		}
		return rb;

	}

	@Override
	public List<String> singleUpload(HttpServletRequest request,
			String suffixDir, String type, Boolean cutPhoto, String merchantId)
			throws Exception {

		List<String> paths = null;
		// 图片上传类型
		if (type.equals(Fs.FileUpLoadType.PHOTO))
		{

			// 判断数据字典里是否有该目录
			Map<String, String> photos = (Map<String, String>) dictService.getDictMap(DictConstants.ClsCode.FS);
			if (!matchDir(photos, suffixDir))
			{
				// suffixDir=FileDir.NOT_DIR;
				
//				path=com.xyt.gsm.constant.Fs.UpoladStatus.文件目录未分配.name();

			} else
			{

				// 商家id /处理
				merchantId = manageSeparator(merchantId);
				// suffixdri
				suffixDir = manageSeparator(suffixDir);

				paths = FileUpload.fileUpload(request, merchantId + suffixDir, Fs.FileUpLoadType.PHOTO, cutPhoto);

			}
		}

		return paths;
	}

}

package com.xyt.gsm.mvc.controller.fs;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonObject;
import com.xyt.gsm.bean.common.ResultBean;
import com.xyt.gsm.bean.mgr.FileUploadVo;
import com.xyt.gsm.bean.mgr.LoginUser;
import com.xyt.gsm.constant.CommonConstants;
import com.xyt.gsm.constant.Fs;
import com.xyt.gsm.constant.Fs.UpoladStatus;
import com.xyt.gsm.entity.mgr.ProductPhoto;
import com.xyt.gsm.entity.mgr.User;
import com.xyt.gsm.mvc.controller.BaseController;
import com.xyt.gsm.service.fs.FileUploadService;
import com.xyt.gsm.service.ice.MerchantService;
import com.xyt.gsm.utils.FileUpload;
import com.xyt.gsm.utils.StringUtil;

/**
 * 图片上传接口 controller
 * 
 * @author liangrui
 * 
 */

@Controller
@RequestMapping(CommonConstants.UriPrefix.PC + CommonConstants.Menu.SYS + "/photoupload")
public class PhotoUploadController extends BaseController
{

	protected Log log = LogFactory.getLog(this.getClass().getName());

	@Resource
	FileUploadService fileUploadService;
	@Resource
	MerchantService merchantService;

	// 多张上传
	@ResponseBody
	@RequestMapping(value = http://www.mamicode.com/"/manyup")>
前端
部分相关代码
商品主图:  
  <div class="upload" id="mainPicButton">
 <form method="post" enctype="multipart/form-data" target="hidden_frame" id="formImg0">
									<!-- <input type="file" name="file0" id="file0" onchange="submitUpload('0')" style="width:68px;height:22px;"> -->
									<input type="file" name="file0" id="file0" onchange="addOnePic('#mainPicButton')" style="width:68px;height:22px;">
									<input type="hidden" name="idnum" value=http://www.mamicode.com/"0">>
<th><span>商品更多图片:</span></th>
             
						<td>
						           
							<div  id="morePicButton">
								<form method="post" enctype="multipart/form-data" target="hidden_frame" id="formImg1">
									<input type="file" name="file0" id="file0" onchange="addMorePic();" style="width:68px;height:22px;">
									<input type="hidden" name="idnum" value=http://www.mamicode.com/"0">>
js:
//全局常量
var maxNoOfPics = 5;
var picPrefix = "/fs"; //图片路径前缀

//全局变量
var imgNum = 0;      //图片数量
var imgNo=0;		//图片ID
var hasMainPic = false;
var origPics = new Array(); //当更新某商品时,保存原来图片的路径,用于在删除时判断是否需要延迟删除

//var domainStr=window.top.getLoginUser().merchantProductDomain;

function getDomain(){
	var domainSt="";
	var userObj=window.top.getLoginUser();
	if(userObj.user.userType=="0")
	{
		domainSt=userObj.publicUploadDomain;
	}else if(userObj.user.userType=="2"){
		domainSt=userObj.merchantProductDomain;
	}
//	alert (domainSt);
	return domainSt;
}

function submitUpload(id){
	$("#imgSrc" + id +"").attr("alt", "图片上传中……");
	var imgID = id;
	if(id>0){
		imgID = 1;
	}
	
	var form=document.getElementById("formImg" + imgID +"");	
	form.method = "post";		
	form.idnum.value = http://www.mamicode.com/id;>
callback-up.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
<script type="text/javascript">

//alert(document.domain);
var returndata=http://www.mamicode.com/self.location.hash.substring(1);>