package com.xxx.zht;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

@Controller
public class InfoSetAction {
	// @Autowired
	// TemplateService templateService;
	@Autowired
	CommonsMultipartResolver multipartResolver;

	// @POST
	// @Path("upload")
	// @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	//@ResponseBody
	@RequestMapping(value = "tempimg")
	public void addCategory(HttpServletRequest request, String path1,
			String path2, HttpServletResponse actioncontext) {
		System.out.println("添加图片");
		String re=null;
		String sub=null;
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile imgFile1 = multipartRequest.getFile("file");// 获取上次文件名
		System.out.println("----->" + imgFile1.getOriginalFilename());
		List<String> fileTypes = new ArrayList<String>();
		fileTypes.add("jpg");
		fileTypes.add("jpeg");
		fileTypes.add("bmp");
		fileTypes.add("gif");
		if (!(imgFile1.getOriginalFilename() == null || "".equals(imgFile1
				.getOriginalFilename()))) {
			String uploadfile = request.getServletContext().getRealPath(
					"imgfile");
			System.out.println("--->" + path1 + "--->" + path2);
			System.out.println("上传路径---->" + uploadfile);
			// File file1 = this.getFile(imgFile1, path2, path1, fileTypes);
			
			File file1 = this.getFile(imgFile1, uploadfile, fileTypes);
			System.out.println("完成上传------>" + file1);
			re=file1.toString();
			//int i=re.lastIndexOf("\\");
			sub=re.substring(re.lastIndexOf("\\")+1);
			System.out.println("截取--->"+sub);
		}
		actioncontext.setContentType("text/html");
		PrintWriter out;
		try {
			out = actioncontext.getWriter();
			out.println("/fileupload/imgfile/" + sub);
			//out.flush();
			//out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//return "imgfile/"+sub;
	}
	
	// private File getFile(MultipartFile imgFile, String typeName,
	private File getFile(MultipartFile imgFile, String brandName, List fileTypes) {
		String fileName = imgFile.getOriginalFilename();
		// 获取上传文件类型的扩展名,先得到.的位置,再截取从.的下一个位置到文件的最后,最后得到扩展名
		String ext = fileName.substring(fileName.lastIndexOf(".") + 1,
				fileName.length());
		System.out.println("获取扩展名---->" + ext);
		// 对扩展名进行小写转换
		ext = ext.toLowerCase();

		File file = null;
		if (fileTypes.contains(ext)) { // 如果扩展名属于允许上传的类型,则创建文件
			// file = this.creatFolder(typeName, brandName, fileName);
			file = this.creatFolder(brandName, fileName);
			System.out.println("完整路径--->" + file);
			try {
				imgFile.transferTo(file); // 保存上传的文件
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return file;
	}

	// private File creatFolder(String typeName, String brandName, String
	// fileName) {
	private File creatFolder(String brandName, String fileName) {
		File file = null;
		// typeName = typeName.replaceAll("/", ""); // 去掉"/"
		// typeName = typeName.replaceAll(" ", ""); // 替换半角空格
		// typeName = typeName.replaceAll(" ", ""); // 替换全角空格

		// brandName = brandName.replaceAll("/", ""); // 去掉"/"
		// brandName = brandName.replaceAll(" ", ""); // 替换半角空格
		// brandName = brandName.replaceAll(" ", ""); // 替换全角空格

		// File firstFolder = new File("c:/" + typeName); // 一级文件夹
		File firstFolder = new File(brandName);
		System.out.println("文件路径--->" + firstFolder);

		String suffix = fileName.substring(fileName.lastIndexOf('.'));
		System.out.println("截取文件后缀--->" + suffix);
		String prefix = System.currentTimeMillis() + "";
		String newfileName = prefix + suffix;
		if (firstFolder.exists()) { // 如果一级文件夹存在,则检测二级文件夹
		// File secondFolder = new File(firstFolder, brandName);
		// if (secondFolder.exists()) { // 如果二级文件夹也存在,则创建文件
		// file = new File(secondFolder, fileName);
		// } else { // 如果二级文件夹不存在,则创建二级文件夹
		// secondFolder.mkdir();
		// file = new File(secondFolder, fileName); // 创建完二级文件夹后,再合建文件
			// file = new File(fileName);
			System.out.println("现在路径---->" + brandName);
			file = new File(brandName + "\\" + newfileName);
			// }
		} else { // 如果一级不存在,则创建一级文件夹
			firstFolder.mkdir();
			// File secondFolder = new File(brandName);
			// if (secondFolder.exists()) { // 如果二级文件夹也存在,则创建文件
			// file = new File(secondFolder, fileName);
			// } else { // 如果二级文件夹不存在,则创建二级文件夹
			// secondFolder.mkdir();
			// file = new File(secondFolder, fileName);
			System.out.println("现在路径---->" + brandName);
			file = new File(brandName + "\\" + newfileName);
			// }
		}
		return file;
	}
}
最近下载更多
wanglipingjxn27  LV1 2021年5月21日
汤汤汤要学编程  LV11 2021年4月23日
zhanss  LV7 2020年5月15日
wangshihua  LV19 2020年5月13日
mengfanyun  LV9 2020年5月1日
wsupsup  LV16 2020年4月16日
pzy12345  LV8 2020年3月30日
小肥羊  LV16 2020年1月10日
昵称zzz  LV13 2019年12月21日
13940562934  LV22 2019年11月14日
最近浏览更多
jwfadacai  LV8 2022年5月11日
zhy1989wz  LV6 2022年3月16日
czr2233  LV9 2021年9月14日
youyou0805  LV8 2021年7月5日
13561143836  LV7 2021年6月20日
1973356987  LV13 2021年6月17日
71503577 2021年6月1日
暂无贡献等级
wanglipingjxn27  LV1 2021年5月21日
wangjiahui 2021年5月20日
暂无贡献等级
anyiyi  LV11 2021年5月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友