不通过网络,就是本地硬盘的文件,应该很简单吧?

解决方案 »

  1.   

    /*
     * 创建日期 2005-6-13
     */
    package cn.ehoo.wap.util;import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.commons.fileupload.DiskFileUpload;
    import org.apache.commons.fileupload.FileItem;/**
     * @author Dirac
     */
    public class UploadFile {
      /**
         * Uploads images from HttpServletRequest
         * 
         * @param request
         * @param basePath
         *            The path of the web context.
         * @return the uploaded image path which is relative to the web context
         */
      public static FileModel uploadFile(HttpServletRequest request, String basePath) {
        long maxSize = 1048576;
        FileModel model = new FileModel();
        DiskFileUpload fu = new DiskFileUpload();
        fu.setSizeMax(maxSize);
            fu.setSizeThreshold(4096);
            fu.setRepositoryPath("/temp");
            Calendar cal = Calendar.getInstance();
            String imagePath = "/upload/" + cal.get(Calendar.YEAR) + "/"
                    + (cal.get(Calendar.MONTH) + 1) + "/"
                    + cal.get(Calendar.DAY_OF_MONTH) + "/";
            String image = "";
            try {
                List list = fu.parseRequest(request);
                Iterator iterator = list.iterator();
                String dir = basePath + imagePath;
                SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhMMss");
                String path = imagePath + format.format(new Date());
                if (!new File(dir).exists()) {
                    new File(dir).mkdirs();
                }
                while (iterator.hasNext()) {
                    FileItem item = (FileItem) iterator.next();
                    String name = item.getName();
                    long size = item.getSize();
                    model.setSize(size);
                    if (size == 0) {
                        continue;
                    }
                    image = path + name.substring(name.lastIndexOf("."));
                    model.setPath(image);
                    item.write(new File(basePath + image));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            model.setExt(image.substring(image.indexOf('.')));
            return model;
        }
    }
    /*
     * 创建日期 2005-7-12
     */
    package cn.ehoo.wap.util;/**
     * @author Dirac
     */
    public class FileModel {
        private long size;    private int width;    private int height;    private String path;    private String ext;    /**
         * @return 返回 path。
         */
        public String getPath() {
            return path;
        }    /**
         * @param path
         *            要设置的 path。
         */
        public void setPath(String path) {
            this.path = path;
        }    /**
         * @return 返回 height。
         */
        public int getHeight() {
            return height;
        }    /**
         * @param height
         *            要设置的 height。
         */
        public void setHeight(int height) {
            this.height = height;
        }    /**
         * @return 返回 size。
         */
        public long getSize() {
            return size;
        }    /**
         * @param size
         *            要设置的 size。
         */
        public void setSize(long size) {
            this.size = size;
        }    /**
         * @return 返回 width。
         */
        public int getWidth() {
            return width;
        }    /**
         * @param width
         *            要设置的 width。
         */
        public void setWidth(int width) {
            this.width = width;
        }
        /**
         * @return 返回 ext。
         */
        public String getExt() {
            return ext;
        }
        /**
         * @param ext 要设置的 ext。
         */
        public void setExt(String ext) {
            this.ext = ext;
        }
    }用的是apache的file-upload.jar
      

  2.   

    多谢楼上的,不过想问一下,为什么一定要用HttpServlet呢,我就是想把本地文件保存到数据库
      

  3.   

    不论是什么文件,都要用JDBC去存,并不难。我那个例子是网站上传文件的一个例子,仅供参考。
      

  4.   

    自己解决了,原来如此的简单Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    Connection conn=java.sql.DriverManager.getConnection(uri,"sa","sa");
    File file=new File("C:\\a.txt");
    FileInputStream fin=new FileInputStream(file);
    PreparedStatement ps;

    ps=conn.prepareStatement("insert into test1 values(?,?)");
    ps.setInt(1,1);
    ps.setBinaryStream(2,fin,(int)file.length());
    ps.executeUpdate();
    ps.close();
    conn.close();
      

  5.   

    qyflaoda(调整心态) ,你的这个办法不能实现文件在客户端的机器上,只能把文件放在服务器上才可以