JSP 用cos组件上传怎么得到文件的大小?
请高手们帮忙一下,怎么样取得文件的大小?
谢谢

解决方案 »

  1.   

    package com.modules;    
       
    import java.io.*;    
    import java.util.*;    
       
    import javax.servlet.*;    
    import javax.servlet.http.*;    
       
    public class FormData {    
        private String f_ex = "gif,bmp,jpg,png"; //允许上传文件类型,以逗号隔开,注意小写    
        private long f_max = 5000; //允许上传文件大小 kb    
        private String f_path = ""; //上传文件存入目录    
        private String f_url = ""; //上传文件返回URL路径    
        private String f_name = String.valueOf((new Date()).getTime()) +    
                                String.valueOf(number(5)); //上传文件名字 f_name+_f_name  _f_name+=1    
       
        private int _f_name = 0;    
        private String fileEx = ""; //扩展名    
        private String key_name; //hash-------key(key_name=表单元素file名)对应文件上传路径    
        private String path = "";    
        private String errcodes = "";    
        Hashtable hash = new Hashtable();    
       
        public int number(int n) { //随机数    
            Random rand = new Random();    
            int i = rand.nextInt();    
            i %= n;    
            while (i == 0) {    
                i = rand.nextInt();    
                i %= n;    
            }    
            if (i < 0) {    
                i = -i;    
            }    
            return (i);    
        }    
       
        public String rtrim(String Str) {    
            return Str.replaceAll("\\s*$|\r|\n", "");    
        }    
       
        public void getFormData(HttpServletRequest req) throws ServletException,    
                IOException {    
            errcodes = "";    
            ServletInputStream stream = null;    
            try {    
                stream = req.getInputStream();    
            } catch (Exception e) {    
                stream.close();    
                return;    
            }    
            int i = 0;    
            byte f_data[] = new byte[4096];    
       
            i = stream.readLine(f_data, 0, f_data.length);    
            String f_bound = (new String(f_data)).trim(); //得到分割符    
            while (i != -1) {    
                String f_line = "";    
                f_data = new byte[4096];    
                i = stream.readLine(f_data, 0, f_data.length);    
                f_line = (new String(f_data)).trim();    
       
                if (f_line.indexOf("\"; filename=\"") != -1 &&    
                    !f_line.    
                    substring(f_line.indexOf("\"; filename=\"") + 13,    
                              f_line.length() - 1).equals("")) {    
                    key_name = f_line.substring(f_line.indexOf("name=\"") + 6,    
                                                f_line.indexOf("\"; filename=\""));    
                    fileEx = f_line.substring(f_line.indexOf("\"; filename=\"") +    
                                              13, f_line.length() - 1);    
                    String filex[] = fileEx.split("\\.");    
                    fileEx = filex[filex.length - 1].toLowerCase();    
                    f_line = "";    
                    f_data = new byte[4096];    
                    i = stream.readLine(f_data, 0, f_data.length); //读取Content-Type    
                    f_line = (new String(f_data)).trim();    
                    if (("," + f_ex + ",").indexOf("," + fileEx + ",") == -1) {    
                        errcodes = "<li>只支持上传" + f_ex + "类型文件";    
                    } else {    
                        i = stream.readLine(f_data, 0, f_data.length); //下跳一行    
                        try {    
                            _f_name += 1;    
                            File file = new File(f_path, f_name +    
                                                 String.valueOf(_f_name) + "." +    
                                                 fileEx);    
                            FileOutputStream fos = new FileOutputStream(file);    
                            DataOutputStream dos = new DataOutputStream(fos);    
                            while (i != -1) {    
                                f_line = "";    
                                f_data = new byte[4096];    
                                i = stream.readLine(f_data, 0, f_data.length);    
                                f_line = (new String(f_data)).trim();    
                                if (f_line.startsWith(f_bound)) {    
                                    break;    
                                }    
                                dos.write(f_data, 0, i);    
                            }    
                            fos.flush();    
                            fos.close();    
                            dos.close();    
                            if (file.length() / 1024 > f_max) {    
                                errcodes = "<li>您上传的文件为" + file.length() / 1024 +    
                                           "KB,超出允许上传最大值 " + f_max + " KB";    
                                file.delete();    
                            }    
                            if (file.exists()) {    
                                path = f_url + f_name + String.valueOf(_f_name) +    
                                       "." +    
                                       fileEx;    
                                hash.put(key_name, path);    
                            }    
                        } catch (Exception e) {    
                            errcodes = "<li>保存文件失败";    
                        }    
                    }    
                } else if (f_line.indexOf(" name=\"") != -1) {    
                    String name = f_line.substring(38, f_line.length() - 1);    
                    i = stream.readLine(f_data, 0, f_data.length);    
                    f_line = "";    
                    while (i != -1) {    
                        f_data = new byte[4096];    
                        i = stream.readLine(f_data, 0, f_data.length);    
                        if ((new String(f_data)).trim().startsWith(f_bound)) {    
                            break;    
                        }    
                        f_line += rtrim(new String(f_data, 0, i, "gb2312")) +    
                                "\r\n";    
                    }    
                    hash.put(name, f_line.substring(0, f_line.length() - 2));    
                }    
            }    
            stream.close();    
        }    
       
        public String getData(String keys) {    
            return (String)this.hash.get(keys);    
        }    
       
        public String getError() {    
            return this.errcodes;    
        }    
       
        public void setPath(String f_path) { //setPath(application.getRealPath("/upload/"));    
            this.f_path = f_path;    
        }    
       
        public String getPath() {    
            return this.f_path;    
        }    
       
        public void setUrl(String f_url) { //setUrl("/upload/");    
            this.f_url = f_url;    
        }    
       
        public String getUrl() {    
            return this.f_url;    
        }    
       
        public void setFex(String f_ex) {    
            this.f_ex = f_ex;    
        }    
       
        public String getFex() {    
            return this.f_ex;    
        }    
       
        public void setMax(long f_max) {    
            this.f_max = f_max;    
        }    
       
        public long getMax() {    
            return this.f_max;    
        }    
    }    
    别忘了结贴~