发布于 2016-01-03 06:08:53 | 304 次阅读 | 评论: 0 | 来源: PHPERZ

这里有新鲜出炉的精品教程,程序狗速度看过来!

Spring Framework 开源j2ee框架

Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transcation Managment,等等


示例代码:

/**
 * 
 */
package com.up.controller;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Random;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.up.constant.SystemConstants;
import com.up.util.FileCopy;
import com.up.util.ImageUtils;
import com.up.util.OperateImage;
import com.up.util.UploadUtil;


/**
 * @author hu.shiguo
 * @time 2014-3-14下午2:53:15
 * @description
 * @version
 */
@Controller
@RequestMapping(value="upload")
public class UploadFileController {
    
	/**
	 * 图片上传
	 * @param request
	 * @param response
	 * @param myFile
	 */
	@RequestMapping(value = "uploadFile")
	public void upload(HttpServletRequest request, HttpServletResponse response, 
			@RequestParam MultipartFile myFile)
	{
		//要删除文件的所在子目录
		String fileFolderpath = request.getParameter("fileFolderpath");  
		//工程目录
		String projectPath  = SystemConstants.WEB_ROOT;
		File files = new File(projectPath);
		//上传文件保存目录
		String uploadFilePath = files.getParent()+File.separator; 
		//String uploadFilePath =files.getPath()+File.separator + "uploadFile";  
		//上传实际路径
		String basePath = uploadFilePath+"uploadFile"+File.separator+"up"+File.separator +fileFolderpath;  
		//未压缩的图片上传至临时文件夹
		String tempPath = uploadFilePath+"uploadFile"+File.separator+"up"+File.separator +SystemConstants.URL_TEMP; 
		InputStream is = null ;
		FileOutputStream os = null;
		if (!new File(basePath).isDirectory()) 
		{
			new File(basePath).mkdirs();
		}
		try
		{
			//获取上传文件旧名
			String name = myFile.getOriginalFilename();
			//获取后缀名
			String last = name.substring(name.lastIndexOf(".")+1); 
			//上传路径--压缩前
			String org = "";
			File file = new File(tempPath, System.currentTimeMillis() + new Random(50000).nextInt() + "."+last);
		    is = myFile.getInputStream();
			os = new FileOutputStream(file); 
			//上传
			UploadUtil.copyFile(is, os);  
			//获取未压缩图片上传后的绝对路径===在临时文件夹目录中 
			org = file.getAbsolutePath();
			System.out.println("org=="+org);
			//压缩后图片存储路径 
			String dest = System.currentTimeMillis() + new Random(50000).nextInt() + "."+last;
			System.out.println("dest=="+dest);
			//System.out.println("file.getName():"+file.getName());  
			//方法一:进行压缩 
			boolean bol1 = ImageUtils.resize(org, basePath+File.separator+dest, 200, 200);  
			//方法二:进行剪切
			//返回压缩后的图片名称到前端展示 
			//先缩放,再裁剪
			boolean bol2 = false;
			if(bol1){
				OperateImage o = new OperateImage(basePath+File.separator+dest, 0, 0, 200, 200);     
				o.setSubpath(basePath+File.separator+dest);
				o.setImageType(last);  
				bol2 = o.cut();   
			} 
			if(bol1||bol2){ 
				System.out.println("---------"+dest); 
				response.getWriter().write(dest);   
			}else{
				FileCopy fc = new FileCopy();
				//因为没有压缩,所以将未压缩的文件从临时文件中复制至目标路径下  
				fc.doMoveFile(file.getName(), tempPath, basePath);
				response.getWriter().write(file.getName()); 
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}finally{
			try{  
				if (os != null) {
					os.close();
				}
				if (is != null) {
					is.close();
				}
			} catch (Exception e) {
					os = null;
					is = null;
			}
		}
	}
	
	
	/**
	 * 删除上传图片
	 * @param request
	 * @param response
	 * @param myFile
	 */
	@RequestMapping(value = "deleteFile.html") 
	public void  deleteFile(HttpServletRequest request,
			HttpServletResponse response)
	{
		String fileFolderpath = request.getParameter("fileFolderpath")+"/"; 
		String fileName = request.getParameter("fileName");
		//工程目录 
		String webPath  = SystemConstants.WEB_ROOT;
		File files = new File(webPath);
		//上传文件的目录
		String uploadFilePath = files.getParent()+File.separator; 
		//String uploadFilePath =files.getPath()+File.separator + "uploadFile";  
		String basePath = uploadFilePath+SystemConstants.URL_UPLOADFILE+fileFolderpath+fileName; 

		File file = new File(basePath);
		if (file.isFile() || file.isDirectory()) 
		{
			file.delete();
		}
	  String str = "true";
	  response.setContentType("text/xml;charset=UTF-8"); 
	  response.setCharacterEncoding("UTF-8");
	  response.setHeader("Cache-Control", "no-cache"); 
	  PrintWriter out;
	  try {
	   out = response.getWriter();
	   out.print(str);// 用于返回对象参数
	  } catch (IOException e) {
	   e.printStackTrace();
	  }
	} 
	
}
 


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务