Browse Source

文件上传下载

git-svn-id: svn+ssh://10.10.101.21/source/platform/platform-b2b@295 f3bf4e98-0cf0-11e4-a00c-a99a8b9d557d
suntg 11 years ago
parent
commit
261eab43d8

+ 88 - 0
src/main/java/com/uas/platform/b2b/core/BaseUtil.java

@@ -0,0 +1,88 @@
+package com.uas.platform.b2b.core;
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.Random;
+
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Component;
+
+@Component
+public class BaseUtil implements ApplicationContextAware {
+	private static ApplicationContext context;
+	
+//	目录结构: -SYSPATH(saas)
+//			  	-PATH(WEB-INF)
+//			  -FILEPATH(自定义附件目录)
+	//系统class加载路径 WEB-INF目录
+	public static String PATH;
+	//系统目录,saas
+	public static String SYSPATH;
+	//系统的附件的路径,默认与项目同级
+	public static String FILEPATH;
+
+	public void setApplicationContext(ApplicationContext applicationContext)
+			throws BeansException {
+		context = applicationContext;
+		setPath();
+		setSysPath();
+		setFilePath();
+	}
+	
+	@SuppressWarnings({ "rawtypes", "static-access" })
+	public static void setPath() {
+		Class classes = context.getClass();
+		String strRealPath = classes.getClassLoader().getResource("").getFile();
+		try {
+			strRealPath = URLDecoder.decode(strRealPath, "UTF-8");
+		} catch (UnsupportedEncodingException e) {
+			e.printStackTrace();
+		}
+		File file = new File(strRealPath);
+		BaseUtil.PATH = file.getParent() + file.separator;
+		if(BaseUtil.PATH.contains("/")){
+			BaseUtil.PATH = "/" + BaseUtil.PATH;
+		}
+	};
+	
+	@SuppressWarnings("static-access")
+	public static void setSysPath() {
+		if(BaseUtil.PATH != null) {
+			File file = new File(BaseUtil.PATH);
+			BaseUtil.SYSPATH = file.getParent() + file.separator;
+		}
+	}
+	
+	@SuppressWarnings("static-access")
+	public static void setFilePath() {
+		if(BaseUtil.SYSPATH != null) {
+			File file = new File(BaseUtil.SYSPATH);
+			BaseUtil.FILEPATH = file.getParent() + file.separator;
+		}
+	}
+
+	/**
+	 * 获得随机长度的字符串
+	 * */
+	public static String randomString(int length) {
+		Random randGen = null;
+		char[] numbersAndLetters = null;
+		if (length < 1) {
+			return null;
+		}
+		if (randGen == null) {
+			randGen = new Random();
+			numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
+			// numbersAndLetters =
+			// ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
+		}
+		char[] randBuffer = new char[length];
+		for (int i = 0; i < randBuffer.length; i++) {
+			randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
+		}
+		return new String(randBuffer);
+	}
+}

+ 20 - 0
src/main/java/com/uas/platform/b2b/dao/AttachDao.java

@@ -0,0 +1,20 @@
+package com.uas.platform.b2b.dao;
+
+import java.util.List;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+import com.uas.platform.b2b.model.Attach;
+
+@Repository
+public interface AttachDao extends JpaRepository<Attach, Long> {
+	
+	/**
+	 * 根据文件ID获取实体类
+	 * @param atId
+	 * @return
+	 */
+	public List<Attach> findAttachByAtId(Long atId);
+
+}

+ 38 - 0
src/main/java/com/uas/platform/b2b/service/AttachService.java

@@ -0,0 +1,38 @@
+package com.uas.platform.b2b.service;
+
+import java.util.List;
+
+import com.uas.platform.b2b.model.Attach;
+import com.uas.platform.b2b.model.FileUpload;
+
+
+public interface AttachService {
+
+	/**
+	 * 根据文件ID获取附件
+	 * @param id
+	 * @return
+	 */
+	public List<Attach> getAttachById(Long id);
+	
+	/**
+	 * 保存一个文件路径
+	 * @param attach
+	 * @return
+	 */
+	public Long saveAttachPath(Attach attach);
+	/**
+	 * 上传附件
+	 * @param uploadItem
+	 * @return
+	 */
+	public String uploadAttach(FileUpload uploadItem);
+	/**
+	 * 生成文件的完整路径
+	 * @param fileName 文件名称
+	 * @param dirName 第一级目录
+	 * @param userName 第二级目录
+	 * @return
+	 */
+	public String getFilePath(String fileName, String dirName, String userName);
+}

+ 91 - 0
src/main/java/com/uas/platform/b2b/service/impl/AttachServiceImpl.java

@@ -0,0 +1,91 @@
+package com.uas.platform.b2b.service.impl;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.util.List;
+import java.util.UUID;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.uas.platform.b2b.core.BaseUtil;
+import com.uas.platform.b2b.dao.AttachDao;
+import com.uas.platform.b2b.model.Attach;
+import com.uas.platform.b2b.model.FileUpload;
+import com.uas.platform.b2b.service.AttachService;
+
+
+@Service
+public class AttachServiceImpl implements AttachService{
+
+	@Autowired
+	private AttachDao attachDao;
+	
+	@Override
+	public List<Attach> getAttachById(Long id) {
+		return attachDao.findAttachByAtId(id);
+	}
+
+	@Override
+	public Long saveAttachPath(Attach attach) {
+		attach.setAtId(null);
+		return attachDao.save(attach).getAtId();
+	}
+
+	@Override
+	public String uploadAttach(FileUpload uploadItem) {
+		String filename = uploadItem.getFile().getOriginalFilename();
+		long size = uploadItem.getFile().getSize();
+		if (size > 104857600) {
+			return "FORBIDEN";//文件过大,上传失败
+		}
+		String path = getFilePath(filename, "postattach", "bussinessCodeImg");
+		File file = new File(path);
+		BufferedOutputStream bos = null;
+		BufferedInputStream bis = null;
+		try {
+			bos = new BufferedOutputStream(new FileOutputStream(file));
+			bis = new BufferedInputStream(uploadItem.getFile().getInputStream());
+			int c;
+			while ((c = bis.read()) != -1) {
+				bos.write(c);
+				bos.flush();
+			}
+			bis.close();
+			bos.close();
+		} catch (Exception e) {
+			e.printStackTrace();
+			return "FAILED";//操作失败
+		}
+		Attach attach = new Attach();
+		attach.setAtPath(path);
+		attach.setAtDescription("客户营业执照复印件或照片");
+		attach.setAtName(filename);
+		Long id = saveAttachPath(attach);
+		return id.toString();
+	}
+
+	@Override
+	public String getFilePath(String fileName, String dirName, String userName) {
+		String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");
+		String suffix = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf("."), fileName.length())
+				: "";
+		String path = BaseUtil.FILEPATH + dirName;
+		File file = new File(path);
+		if (!file.isDirectory()) {
+			file.mkdir();
+			path = path + File.separator + userName;
+			new File(path).mkdir();
+		} else {
+			path = path + File.separator + userName;
+			file = new File(path);
+			if (!file.isDirectory()) {
+				file.mkdir();
+			}
+		}
+		return path + File.separator + uuid + suffix;
+	}
+
+}