package com.hxkj.fsslr.core.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

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

public class MultipleFileUtils {

	/ * * *@Title: uploadOneMultipleFile 
	* @Description: TODO single file upload *@param request	
	* @paramFileProUrl File path *@paramFileProName Saves the file before the name *@paramNewName Specifies whether to generate a new file name *@throws Exception   
	* @returnString File path URL *@author [email protected]
	* @dateApril 19, 2016 1:37:27 PM */
	public static String uploadOneMultipleFile(HttpServletRequest request,String fileProUrl,boolean isNewName) throws Exception{
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Iterator<String> iter=multipartRequest.getFileNames();
		MultipartFile inputFile = multipartRequest.getFile(iter.next());
		String realName = inputFile.getOriginalFilename();
		if(isNewName == true) {// Whether to generate a new file name
			String ext = realName.substring(realName.lastIndexOf("."));
			realName = TimeUtils.getImageNamePrefix() + ext;
		}
		File file = makeDirsAndCreateFileName(fileProUrl,realName);
		inputFile.transferTo(file);
		String realUrl = fileProUrl + "/" + realName;
		return realUrl;
	}

	
	/ * * *@Title: uploadManyMultipleFile 
	* @DescriptionTODO uploads multiple files@param request
	* @paramFileProUrl File save path folder *@paramFileProName The front of the file name *@paramNewName whether to save the newName *@throws Exception   
	* @returnList<String> Path to upload files List *@author [email protected]
	* @dateApril 19, 2016 1:43:48 PM */
	public static List<String> uploadManyMultipleFile(HttpServletRequest request,String fileProUrl,boolean isNewName) throws Exception{
		List<String> realUrls = new ArrayList<>();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Iterator<String> iter=multipartRequest.getFileNames();
		while(iter.hasNext()){
			MultipartFile inputFile = multipartRequest.getFile(iter.next());
			String realName = inputFile.getOriginalFilename();
			if(isNewName == true) {// Whether to generate a new file name
				String ext = realName.substring(realName.lastIndexOf("."));
				realName = TimeUtils.getImageNamePrefix() + ext;
			}
			File file = makeDirsAndCreateFileName(fileProUrl,realName);
			inputFile.transferTo(file);
			String realUrl = fileProUrl + "/" + realName;
			realUrls.add(realUrl);
		}
		return realUrls;
	}
	
	/ * * * *@Title: makeDirsAndCreateFileName 
	* @Description: TODO creates an empty file and does not occupy space *@paramPath Storage path *@paramFileName fileName *@returnFile is an empty File *@author [email protected]
	* @dateApril 19, 2016 1:00:55 PM */
	private static File makeDirsAndCreateFileName(String path,String fileName){
		File file = new File(path);
		if(! file.exists()){ file.mkdirs(); } file =new File(path,fileName);
		returnfile; }}Copy the code