请教各位大虾,我对上传和下载的基本原理和思想不是很清楚,还有就是关于数据库方面的。比如现在我有4个类别,每个类别分别对应相应的文件,数据库是存放文件的目录(真实路径还是。。)还是文件名称抑或是两者都存,或者还有4个类别就分别建立4个文件夹来存储用户上传的文件吗?还是用其他的方法?能不能提供一个详细的数据库表结构,让小弟参考一下,谢谢,不甚感激!

解决方案 »

  1.   

    下载用继承于DownloadAction类
    上传用FormFile类,记得下载Commons FileUpload包
      

  2.   

    上传的action
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.loner.struts.action;import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.upload.FormFile;import com.loner.struts.form.FileForm;/** 
     * MyEclipse Struts
     * Creation date: 12-08-2007
     * 
     * XDoclet definition:
     * @struts.action path="/file" name="fileForm" input="/file.jsp" scope="request" validate="true"
     */
    public class FileAction extends Action {
    /*
     * Generated Methods
     */ /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {

    FileForm fileForm = (FileForm) form;

    String dir = servlet.getServletContext().getRealPath("/upload");//获得一个目录,这个目录


    FormFile file = fileForm.getFile();//取得文件的值


    String fname = file.getFileName();//取的文件名


    String fsize = Integer.toString(file.getFileSize())+"bytes";//取得文件大小

    try{
    InputStream streamIn = file.getInputStream();//构建一个流对象

    OutputStream streamout = new FileOutputStream(dir+"/"+fname);


    int bytesRead = 0;

    byte[] buffer = new byte[8192];//定义一个字节数组
    while((bytesRead=streamIn.read(buffer,0,8192))!=-1){//如果还有内容就写入定义的变量中


    streamout.write(buffer,0,bytesRead);//利用输出流对象输出 }
    streamout.close();
    streamIn.close(); }catch(Exception ex){
    ex.printStackTrace();
    }

    return null;
    }
    }
    FileForm/*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     */
    package com.loner.struts.form;import javax.servlet.http.HttpServletRequest;
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.upload.FormFile;/** 
     * MyEclipse Struts
     * Creation date: 12-08-2007
     * 
     * XDoclet definition:
     * @struts.form name="fileForm"
     */
    public class FileForm extends ActionForm {
    /*
     * Generated fields
     */ /** file property */
    private FormFile file;//该字段的类型必须是org.apache.struts.upload.FormFile类型
    private String fileName;
    private String size;
    /*
     * Generated Methods
     */ /** 
     * Method validate
     * @param mapping
     * @param request
     * @return ActionErrors
     */
    public ActionErrors validate(ActionMapping mapping,
    HttpServletRequest request) {
    // TODO Auto-generated method stub
    return null;
    } /** 
     * Method reset
     * @param mapping
     * @param request
     */
    public void reset(ActionMapping mapping, HttpServletRequest request) {
    // TODO Auto-generated method stub
    } public FormFile getFile() {
    return file;
    } public void setFile(FormFile file) {
    this.file = file;
    } public String getFileName() {
    return fileName;
    } public void setFileName(String fileName) {
    this.fileName = fileName;
    } public String getSize() {
    return size;
    } public void setSize(String size) {
    this.size = size;
    } /** 
     * Returns the file.
     * @return String
     */}
    html页面<%@ page language="java" contentType="text/html; charset=gbk"%>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%><%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
    <jsp:useBean id="fileForm" class="com.loner.struts.form.FileForm"></jsp:useBean>
    <html> 
    <head>
    <title>JSP for FileForm form</title>
    </head>
    <body>
    <html:form action="/file" method="post" enctype="multipart/form-data">
    file : <html:file property="file"/><html:errors property="file"/><br/>
    <html:submit/><html:cancel/>
    </html:form>
    </body>
    </html>你数据库里存放的是文件相对路径最好,还有比如id,文件名,上传时间,上传用户,等等,按照你需要的来吧
    在你程序里新建立个文件夹,用来保存上传来的文件
      

  3.   

    数据库存文件可以用BLOB直接存成二进制也可以村文件的路径,
    用二进制的话,可以用流直接导入数据库存储下来, 读的时候就再用流导出来即可
    用文件路径的话,数据库里只是存一个字符串而已,数据库对这个文件的存在并不知道。
    读出时,先从数据库中读出文件的路径,然后根据这一路径读出文件即可。至于lz说的4个类 就建4个文件夹 这完全取决与程序员的选择,你可以建或者不建,
    甚至你把文件直接放在C盘根目录,只要数据库中的路径保存正确,你就可以正确读出该文件
      

  4.   

    http://blog.csdn.net/hy0231/archive/2008/12/19/3555957.aspx
    这里很清楚。
      

  5.   

    我刚刚做了这个模块!全部代码提供,全面支持!顶 305954240 兄弟多多给我分呀 也是struts的       servlet+jsp的做过,简单的
      

  6.   

    上传下载我就不说了...>_<
    数据库结构我说下我的想法
    你有4个类别..那你建个类别表...
    很简单的表..typeId 主键, typeName 名称, typeFilePath 这个类型的文件夹路径, ...还有你可以加几个re注释...这样方便以后有第5,第6...类别的增加.
    再建个文件表
    fileId 主键,userId 用户, typeId(外键类别的type) ,fileName 文件名称,还可以存入文件大小等信息...
      

  7.   

    下载用DownloadAction类 
    上传用FormFile类,需要下载Commons FileUpload包
      

  8.   

    小海。。你也来啦。过来给你加加油。HOHO~~~~
      

  9.   

    表结构:
    jsp页面主要用:
     <html:file property="csvFile" size="30" style="width:333px; margin-bottom:1px;  "/>
    Action:FormFile file = yourActionForm.getcsvFile();
    String fileName = file.getFileName();
    InputStream is = file.getInputStream();
    File upLoadFile = Utils.uploadFile(is,filePath+fileName);
    UploadFileTbl fileInfo = new UploadFileTbl();
    FileTypeMap map = FileTypeMap.getDefaultFileTypeMap();
    fileInfo.setMime_type(map.getContentType(upLoadFile));
    fileInfo.setFile_name(fileName);
    fileInfo.setFile_data(new FileInputStream(upLoadFile.toString()));
    fileInfo.setReg_user(yourActionForm.getRegistUserId());
    fileInfo.setFKey(FKey);
    fileInfo.setPKey(PKey);
    dao.Insert(fileInfo);要用到的方法:Utils.uploadFile(is,filePath+fileName)public static final File uploadFile(InputStream is,String fname) throws IOException{
    File upLoadFile = new File(fname);
    OutputStream os  =new FileOutputStream(upLoadFile);
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while((bytesRead = is.read(buffer,0,8192)) != -1){
    os.write(buffer,0,bytesRead);
    }
    os.flush();
    is.close();
    os.close();
    return upLoadFile;
    }要用到的Bean:public class UploadFileTbl implements Serializable  {

    private String FKey = null;

    private String PKey = null;

    private String file_name = null;

    private transient InputStream file_data = null;

    private String mime_type = null;  

    private String reg_user = null ;

    private String reg_date = null;      。。
    }dao的Insert方法:private static final String INSERT_FILE = 
          "INSERT INTO FILETABLE( FKEY,PKEY,"
          + "FILE_NAME,FILE_DATA,REGIST_USER_ID,REGIST_DATE,MIME_TYPE,MODIFY_USER_ID,MODIFY_DATE) VALUES"
          + "(?,?,?,?,?,SYSDATE,?,?,SYSDATE)";
    .........
    PreparedStatement pstmt = null;
    InputStream is = null;
    .......
    pstmt = con.prepareStatement(INSERT_FILE);

    pstmt.setString(1, fileInfo.FKey);
    pstmt.setString(2, fileInfo.PKey);
    pstmt.setString(3, fileInfo.getFile_name());

    is = fileInfo.getFile_data();
    pstmt.setBinaryStream(4, is, is.available());
    pstmt.setString(5, fileInfo.getReg_user());
    pstmt.setString(6, fileInfo.getMime_type());
    pstmt.setString(7, fileInfo.getReg_user());
    pstmt.executeUpdate();
      

  10.   

    下载,这个是点链接下载
    Action:String strPKey= request.getParameter("PKey");
    UploadFileTbl file= dao.getFileByKey(strPKey);
    InputStream is = file.getFile_data();
    Utils.saveFile(is, response, file.getFile_name());用到的Utils.saveFile(is, response, file.getFile_name()),我这个是支持日文文件名,编码你到时候可能要改public static void saveFile(InputStream is,HttpServletResponse response,
    String fileName){
    try {
    byte[] src = fileName.getBytes("Windows-31J");
    String fname = new String(src,"ISO8859-1"); 
    response.setContentType("text/plain; charset=Windows-31J");
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition",
    "attachment; filename=" + fname);
    OutputStream os = response.getOutputStream();

    byte buffer[] = new byte[8192];
    int size;
    while ((size = is.read(buffer)) != -1) {
    os.write(buffer, 0, size);
    }
    os.flush();
    is.close();
    os.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }dao的方法public UploadFileTbl getFileByKey(String strKey) throws SQLException,
    ClassNotFoundException {

    UploadFileTbl info = new UploadImageTbl();

    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    try {
    con = getConnection();

    pstmt = con.prepareStatement(SELECT_FILE_BY_KEY);
    pstmt.setString(1, strKey);
    rs = pstmt.executeQuery();

    if(rs.next()){
    info.setFile_name(rs.getString("FILE_NAME"));
    info.setFKey(rs.getString("FKey"));
    info.setReg_date(rs.getString("REGIST_DATE"));
    info.setReg_user(rs.getString("REGIST_USER_ID"));
    info.setPKey(rs.getString("PKey"));
    info.setFile_data(rs.getBinaryStream("FILE_DATA"));
    }
    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    if(rs != null){
    rs.close();
    }
    if (pstmt != null) {
    pstmt.close();

    if (con != null && !con.isClosed()) {
    con.close();
    con = null;

    }

    return info;
    }jsp页面,需要先将数据查出,赋给ActionForm相应的属性:<a href="#" onClick="openImage('<bean:write name="yourActionForm" property="pkey"/>','openTempImg')">
            <bean:write name="yourActionForm" property="file_name"/>
            </a>