请问在web开发中,上传文件怎么做。

解决方案 »

  1.   

    上传文件有好几种,你看你用什么了,具体的话应该从网上搜索具体的,你用google搜索文件上传看看你能搜索到多少结果
      

  2.   

    fileupload   
    Struts中也有上传组件
      

  3.   

    看你想用什么做。php几行代码就可以搞定,JAVA稍微麻烦点。上传一个文件好做,想批量上传文件有点复杂,主要看LZ想做什么,用什么做。
      

  4.   

    http://www.phpletter.com/Demo/AjaxFileUpload-Demo/
      

  5.   

    比较好用的一个是smartupload。自己搜索一下
      

  6.   

    struts有一个上传的主键<html:file>
      

  7.   

    看你是用那种方式进行上传了,如果用普通的方式,需要导入SmartUpload.jar包;
    如果是StrutsMVC模式就用其框架自带的上传组件,或导入fileupload.jar包.
      

  8.   

    第一次问没个结果,第二次也是一样的,加分都到200了、
    网上的很杂,我想用到struts+hibernate+spring 项目中。
    就没有详细点的方法吗。
      

  9.   

    jsp页面:
    <body>
        <br>
        <html:form action="upload.do" method="post" enctype="multipart/form-data">
          <html:file property="imgFile"> </html:file> <br>
          <html:text property="imgName" value=""> </html:text> <br>
          <html:submit value="上传"> </html:submit>
        </html:form> 
    </body>struts-config.xml如下:
    <form-beans>
        <form-bean name="upfileFrom" type="org.apache.struts.validator.DynaValidatorForm">
            <form-property name="imgName" type="java.long.String"> </form-property>
            <form-property name="imgFile" type="org.apache.struts.upload.FormFile"> </form-property>
        </form-bean>
      </form-beans><action-mappings>
        <action
        path="/upload"
        name="upfileFrom"
        input="/index.jsp"
        validate="false"
        type="org.springframework.web.struts.DelegatingActionProxy"
        >
    </action>
    </action-mappings> 
      

  10.   


      既然你用到了 SSH ,那就用 commons-fileupload 上传吧,用 smartupload 也可以,前提条件是在工程目录下存在  
       commons-fileupload.jar,可能还会需要 commons-beanutils.jar
     commons-fileupload 上传代码:<%@ page language="java"
    import="java.util.*,java.io.*,org.apache.commons.fileupload.*"
    pageEncoding="GBK"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>My JSP 'uploadJSPBack.jsp' starting page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <%
    response.setCharacterEncoding("GBK");
    String uploadPath = "";
    String tempPath = "d:\\";
    try {
    System.out.println("开始进行文件上传");
    DiskFileUpload fu = new DiskFileUpload();
    fu.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
    fu.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
    fu.setRepositoryPath(tempPath); // 设置临时目录
    List fileItems = fu.parseRequest(request); // 得到所有的文件:
    Iterator i = fileItems.iterator();
    // 依次处理每一个文件:
    while (i.hasNext()) {
    FileItem fi = (FileItem) i.next();
    String fileName = fi.getName();// 获得文件名,这个文件名包括路径:
    if (fileName != null) {
    // 在这里可以记录用户和文件信息
    // 此处可以定义一个接口(CallBack),用于处理后事。
    // 写入文件a.txt,你也可以从fileName中提取文件名:
    String name = fileName.substring(0, fileName
    .indexOf("."));
    String extfile = fileName.substring(fileName
    .indexOf(".")); //上传时间作为文件名,用以防止重复上传
    //Timestamp now = new Timestamp((new java.util.Date()).getTime());
    //SimpleDateFormat   fmt   =   new   SimpleDateFormat("yyyyMMddHHmmssSSS");   
    //String  pfileName=   fmt.format(now).toString().trim();   
    System.out.println(name + extfile);
    fi.write(new File(name + extfile));
    }
    } System.out.println("上传成功"); response.setContentType("text/html;charset=utf-8");
    response.getWriter().print("{success:true,message:'上传成功'}");
    // 跳转到上传成功提示页面
    } catch (Exception e) {
    e.printStackTrace();
    response.getWriter().print("{success:false,message:'上传失败'}");
    // 可以跳转出错页面
    }
    %>
    </head> <body>
    </body>
    </html> smartupload.jar 上传代码: 当然前提条件是在你的工程下存在 jspsmartupload.jar<%@ page contentType="text/html; charset=GBK" %><html>
    <head>
    <title>
    uploadPic
    </title>
    <style type="text/css">
    <!--
    .STYLE1 {
    font-size: 12px;
    color: #0066FF;
    }
    -->
    </style>
    <script type="">
       function check(){
         var filePath1 = document.form1.file.value;
         var filePath2 = document.form1.file2.value;
         var filePath3 = document.form1.file3.value;     if((filePath1 == "") && (filePath2 == "") && (filePath3 == "")){
           alert("您还没有选择上传的图片呢!");
           return false;
         }
         else{
           return true;
         }
       }
    </script>
    </head>
    <body bgcolor="#ffffff">
    <h1 align="center">广告信息的图片上传</h1>
    <form action="Upload.jsp" method="post" enctype="multipart/form-data"  name="form1" onsubmit="return check()">
      <table width="420" height="169" align="center" cellpadding="0" border="1" cellspacing="0" style="border-bottom:1px solid">
      <caption>选择多张图片后点击上传即可</caption>
        <tr>
          <td width="74"><div align="center"><span class="STYLE1">图片1</span>:</div></td>
          <td width="340"><input name="file" type="file" size="30"></td>
        </tr>
       <tr>
          <td height="31"><div align="center" class="STYLE1">图片2:</div></td>
          <td><input name="file2" type="file" size="30"></td>
        </tr>
        <tr>
          <td><div align="center" class="STYLE1">图片3:</div></td>
          <td><input name="file3" type="file" size="30"></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input type="submit" name="Submit" value="上传">
          <input type="reset" name="Submit2" value="取消"></td>
        </tr>
      </table>
    </form>
    <p>&nbsp;</p>
    </body>
    </html><%@page contentType="text/html; charset=GBK"%>
    <%@page import="com.jspsmart.upload.*"%>
    <html>
    <head>
    <title>Upload</title>
    </head>
    <body bgcolor="#ffffff">
    <%
      /* 用此组件上传文件,若存在相同名称的文件,它会自动选择覆盖  */  try {
        request.setCharacterEncoding("GBK");    SmartUpload su = new SmartUpload();    int countUpload = su.getFiles().getCount();    //初始化页面作用域
        su.initialize(pageContext);    su.service(request, response);    //限制上传文件的类型,允许格式为 jpg,gif,bmp
        su.setAllowedFilesList("jpg,gif,bmp");    //禁止文件上传类型
        su.setDeniedFilesList("exe,bat,jsp,htm,html,");    su.upload();    //记录成功上传图片的数目
        int count =0;
        /*
         *  如果想上传的图片以自己定义的名称保存的话,使用以下的方法
         *
         *  注意:使用这种方式保存的话,下面的 su.save("/image"); 此方法就不需要了,否则就重复保存了    int size = su.getFiles().getCount();
        for(int i=0;i<size;i++){
          File file = su.getFiles().getFile(i);
          file.saveAs("/image/"+(i+1)+".jpg");
          count++;
        }*/    //将这些图片保存到站点下的 image 文件夹下,并得到成功上传文件数目
        count = su.save("/image");    out.println("<script>");
        out.println("alert('成功上传图片 "+count+" 张!');history.back(1);");
        out.println("</script>");
      }
      catch (SmartUploadException ex) {
        out.print(ex.toString());
      }
      catch(Exception e){
        out.println("<script>");
        out.println("alert('存在不允许上传的格式,只允许上传后缀为 jpg|gif|bmp 格式的图片,请重新选择文件!');history.back(1);");
        out.println("</script>");
      }
    %>
    </body>
    </html>