文件上传


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.log4j.Logger;

import com.duosen.gate.listener.ApplicationContextListener;

/**
 * @项目名称:56gate
 * @类名称:UploadAndDownloadFileUtils
 * @类描述:图片下载
 * @创建人:zr
 * @创建时间:2013-8-19 下午08:10:05
 * @修改人:zr
 * @修改时间:2013-8-19 下午08:10:05
 * @修改备注:
 * @version 1.0
 */
public class UploadAndDownloadFileUtils {
    
    private static final Logger LOG = Logger.getLogger(UploadAndDownloadFileUtils.class);


    public static String uploadFile(File file, String fileName, final String savepath) {

        String url = null;
        InputStream is = null;
        OutputStream os = null;
        try {

            is = new FileInputStream(file);

            String name = getNewName(fileName);

            String root = ApplicationContextListener.getServletContext().getRealPath("/" + savepath);

            url = savepath + "/" + name;

            File destFile = new File(root, name);

            os = new FileOutputStream(destFile);

            byte[] buffer = new byte[1240];

            int length = 0;

            while ((length = is.read(buffer)) > 0)
            {
                os.write(buffer, 0, length);
            }

        } catch (Exception e) {
            url = null;
            System.err.println("上传文件异常!" + e);
        }finally {
            try {
                if(is!=null){
                    is.close();
                }
                if(os!=null){
                    os.close();
                }

            } catch (IOException e) {
                url = null;
                LOG.error("输入输出流关闭异常!" + e);
            }

        }


        return url;
    }

    /**重新命名文件*/
    private static String getNewName(String fileName)
    {
        int pos = fileName.lastIndexOf(".");
        String name = String.valueOf(getCurrentTimeMillis());
        name = name + fileName.substring(pos);        
        return name;
    }

    /**确保返回时间片唯一*/
    public static synchronized long getCurrentTimeMillis() {
        try {
            Thread.sleep(100);
        } catch (Exception e) {
            LOG.error("线程休眠异常!" + e);
        }
        return System.currentTimeMillis();
    }
    
    /**
     * 下载图片
     */
    public static String downloadImg(String url,String path){
        
        System.out.println("url-===================================="+url);
        String savePath = null;
        try {
            URL con = new URL(url.replaceAll(":100", ""));
            URLConnection conn = con.openConnection();
            InputStream is = conn.getInputStream();
            String fileName = getNewName(url);
            savePath =  path + fileName;
            fileName = ApplicationContextListener.getServletContext().getRealPath(savePath);
            BufferedOutputStream  fos = new BufferedOutputStream(new FileOutputStream(fileName));
            byte[] buffer = new byte[10240];
            int length = 0;
            while (-1 != (length =is.read(buffer, 0, buffer.length))){
                fos.write(buffer, 0, length);
            }
            fos.flush();
            fos.close();
            is.close();
            if (fos != null){
                fos.close();
            }
            if (is != null){
                is.close();
            }
        } catch (Exception e) {
            savePath = null;
            LOG.error("下载图片异常!", e);
        }        
        return savePath;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值