`
stephen830
  • 浏览: 2969230 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

上传下载组件SmartUpload使用方法

    博客分类:
  • java
阅读更多
★★★ 本篇为原创,需要引用转载的朋友请注明:《 http://stephen830.iteye.com/blog/255010 》 ,谢谢支持!★★★

我喜欢实用性的东西,不喜欢理论性的东西,尤其对越来越多的新概念不是很感冒。因此,我的文章主要以实用性为目标,让需要的朋友切切实实的能解决问题。

本篇将讲述java常用的上传/下载组件 SmartUpload 的详细java的朋友不会陌生,几乎在所有的B/S架构的项目或者产品中都会用到文件的上传/下载。

关于SmartUpload组件可以在本篇下面的附件中下载。(已经解决上传/下载中文文件名的乱码问题,本人的开发环境均为UTF-8环境)

(1)文件上传

<%@ page contentType="text/html;charset=UTF-8"%><%@ page import="com.soft4j.httpupload4j.SmartUpload"%><%@ page import="com.soft4j.bo.WebFileMgr"%><%
	String ret = null;
	SmartUpload su = null;
	try{
		su = new SmartUpload();
		su.initialize(pageContext);
		su.upload();
		ret = WebFileMgr.upload(su,pageContext);//WebFileMgr.java 处理文件上传
		if(ret!=null){ 
			out.print("successed");
		}
	}catch(Exception e){
		out.print("faild");
	}finally{
		su = null;
	}
%>


WebFileMgr.upload(su,pageContext)方法内容:
    /**
	 * 上传方法.
	 * @param su
	 * @param pageContext
	 * @return
	 * @throws Exception
	 */
    public static String upload(SmartUpload su,PageContext pageContext) throws Exception {
        com.soft4j.httpupload4j.File suFile = null;
        int fileCount = 0;
        int maxFileSize = 900;//单个文件最大为900K
        String AllowedExtensions=",jpg,jpeg,gif,png,";//允许上传的文件类型
        try {
            
            for (int i=0; i<su.getFiles().getCount();i++) {
                suFile = su.getFiles().getFile(i);
                if (suFile.isMissing())
                    continue;
                fileSize = suFile.getSize()/1024;//字节转换成KB
                if(fileSize==0) fileSize=1;

                if(maxFileSize<fileSize) throw new Exception("单个上传文件的容量不能超过["+maxFileSize+"KB]");

                if (suFile.getFileExt() == null
                        || "".equals(suFile.getFileExt())) {
                    fileExt = ",,";
                } else {
                    fileExt = "," + suFile.getFileExt().toLowerCase() + ",";
                }

                if (!"".equals(AllowedExtensions)
                        && AllowedExtensions.indexOf(fileExt) == -1) {
                    throw new Exception("您上传的文件[" + suFile.getFileName()
                            + "]的类型为系统禁止上传的文件类型,不能上传!");
                }

                fileCount++;
            }
            if (fileCount==0) throw new Exception("请选择上传的文件");

            StringBuffer fullFileName = null;//保存到服务器上的文件名(带路径)
            for (int i=0; i<su.getFiles().getCount();i++) {
                suFile = su.getFiles().getFile(i);
                if (suFile.isMissing()) continue;
                fullFileName = new StringBuffer("文件的路径");//填写 文件的路径
                fullFileName.append("文件名");//填写 文件名
                suFile.saveAs(fullFileName.toString(),SmartUpload.SAVE_PHYSICAL);
            }
            return "successed";
        } finally {
        	//
        }
    }



上面的JSP,Java代码就可以实现文件的上传.

(2)文件下载

下载相对更为简单,只要设定下载的文件名(带路径)就可以了.

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="com.soft4j.httpupload4j.SmartUpload"%>
<%
	//新建一个SmartUpload对象
    SmartUpload su = new SmartUpload();
    //初始化
    su.initialize(pageContext);
    //设定要下载的文件名(带路径)
    String fileName="下载的文件名";//文件名(带路径)
    try{
    	su.downloadFile(fileName);//下载文件
    }catch(Exception e){
    	e.printStackTrace();
    	out.println(e.toString());
    }
    response.getOutputStream().close();
%>





-----------------------
附录:
(1). 解决中文问题的修改说明.参见[zip压缩包中的SmartUpload.java]
(2). smartupload.zip








分享到:
评论
6 楼 huangleizzz 2012-02-16  
  
5 楼 duronshi 2009-01-09  
这里只判断了文件的后缀,最好是通过判断文件头来校验上传文件的合法性.
4 楼 stephen830 2009-01-08  
javacoo 写道
怎么得到图片的宽和高呢?


/**
	 * 生成缩略图
	 * @param srcImageFile 源图片文件的File实例.
	 * @param dstImageFileName 待生成的缩略图片文件名
	 * @throws Exception
	 */
	public static void makeSmallImage(File srcImageFile,String dstImageFileName) throws Exception {
		FileOutputStream fileOutputStream = null;
        JPEGImageEncoder encoder = null;
        BufferedImage tagImage = null;
        Image srcImage = null;
		try{
	        srcImage = ImageIO.read(srcImageFile);
	        int srcWidth = srcImage.getWidth(null);//原图片宽度
	        int srcHeight = srcImage.getHeight(null);//原图片高度
	        int dstMaxSize = 120;//目标缩略图的最大宽度/高度,宽度与高度将按比例缩写
	        int dstWidth = srcWidth;//缩略图宽度
	        int dstHeight = srcHeight;//缩略图高度
	        float scale = 0;
	        //计算缩略图的宽和高
	        if(srcWidth>dstMaxSize){
	            dstWidth = dstMaxSize;
	            scale = (float)srcWidth/(float)dstMaxSize;
	            dstHeight = Math.round((float)srcHeight/scale);
	        }
	        srcHeight = dstHeight;
	        if(srcHeight>dstMaxSize){
	            dstHeight = dstMaxSize;
	            scale = (float)srcHeight/(float)dstMaxSize;
	            dstWidth = Math.round((float)dstWidth/scale);
	        }
	        //生成缩略图
	        tagImage = new BufferedImage(dstWidth,dstHeight,BufferedImage.TYPE_INT_RGB);
	        tagImage.getGraphics().drawImage(srcImage,0,0,dstWidth,dstHeight,null);
	        fileOutputStream = new FileOutputStream(dstImageFileName);
	        encoder = JPEGCodec.createJPEGEncoder(fileOutputStream);
	        encoder.encode(tagImage);
	        fileOutputStream.close();
	        fileOutputStream = null;
		}finally{
			if(fileOutputStream!=null){
        		try{
        			fileOutputStream.close();
        		}catch(Exception e){
        		}
        		fileOutputStream = null;
        	}
        	encoder = null;
        	tagImage = null;
        	srcImage = null;
            System.gc();
		}
	}
3 楼 javacoo 2009-01-08  
怎么得到图片的宽和高呢?
2 楼 stephen830 2008-10-26  
Nick_HF 写道

在客户哪里使用,Cpu占用率比较高,后来换了,建议用apache common upload组件


后台具体用哪个实现可以自行更替,samrtupload是先写内存再存文件,所以占资源多点但效率高;后者是边传边存文件,占资源少但效率会慢一点。
1 楼 Nick_HF 2008-10-26  
在客户哪里使用,Cpu占用率比较高,后来换了,建议用apache common upload组件

相关推荐

Global site tag (gtag.js) - Google Analytics