java上传文件

来源:互联网 发布:网络虚拟信用卡申请 编辑:程序博客网 时间:2024/06/15 00:46
    public Boolean uploadFiles(File files, String filenames, String newfilenames,String url)            throws IOException {        String root = "";        boolean t = true;        root = ServletActionContext.getServletContext().getRealPath("/systemfile/picture");        try{            FileOutputStream fos = new FileOutputStream(root+"/"+newfilenames);            FileInputStream fis = new FileInputStream(files);            byte[] buffer = new byte[1024];            int len = 0;            while ((len = fis.read(buffer)) > 0) {                fos.write(buffer, 0, len);            }            fis.close();            fos.close();            /* 这儿填写你转化后的图片存放的文件夹 */            ImageChange r = new ImageChange();                String xx = newfilenames.substring(newfilenames.lastIndexOf("."), newfilenames.length());                if(ImageUtil.isImage(new File(root +"/"+ newfilenames))){                    if(xx.indexOf("gif")>-1||xx.indexOf("GIF")>-1){                        FileOutputStream fos1 = new FileOutputStream(root+"1/"+newfilenames);                        FileOutputStream fos2 = new FileOutputStream(root+"2/"+newfilenames);                        FileInputStream fis1 = new FileInputStream(files);                        FileInputStream fis2 = new FileInputStream(files);                        int len1 = 0;int len2 = 0;                        while ((len1 = fis1.read(buffer)) > 0 && (len2 = fis2.read(buffer)) > 0) {                            fos1.write(buffer, 0, len1);                            fos2.write(buffer, 0, len2);                        }                        fis1.close();                        fis2.close();                        fos1.close();                        fos2.close();                    } else {                        int width = Integer.valueOf(MyProperties.getByKey("width"));                        int width2 = Integer.valueOf(MyProperties.getByKey("width2"));                        /* 这个参数是要转化成的宽度 */                        BufferedImage f = r.getBufferedImage(root +"/"+ newfilenames);                        url=root +"/"+ newfilenames;                        r.writeHighQuality(r.zoomImage(f, width), root +"1/", newfilenames);                        r.writeHighQuality(r.zoomImage(f, width2), root +"2/", newfilenames);                        f.flush();                    }                }        } catch (Exception ex) {            System.out.println("error:"+ex);            t = false;        }        return t;    }
原创粉丝点击