不冲突啊。文件无非就是一个大字段,也不过是个字段而已。可以随FORM一起提交的

解决方案 »

  1.   

    给你占段代码
    前端页面的form需要如下设置
    <form name="newsadd_form" action="NewsInsert.jsp" method="post" enctype="multipart/form-data" onSubmit="return checkForm();">
          </form>
    后台代码如下:需要下载commons-fileupload.jar这个包,放在WEB-INF/lib目录下
    <%@ page contentType="text/html; charset=GBK" language="java"%>
    <%@page import="org.apache.commons.fileupload.*,java.io.File,java.io.IOException"%>
    <%@page import="java.util.List"%>
    <%@page import="java.util.Iterator"%>
    <%@page import="java.text.DecimalFormat"%>
    <%@page import="com.linuxok.com.general.UUIDHexGenerator"%>
    <%@page import="com.filemanage.interfaces.INewsFilesDAO"%>
    <%@page import="com.filemanage.factory.NewsFilesFactory"%>
    <%@page import="com.linuxok.com.general.FormatDate"%>
    <%@page import="com.filemanage.dto.News"%>
    <%@page import="com.filemanage.factory.NewsFactory"%>
    <%@page import="com.filemanage.dto.Channel"%>
    <%@page import="com.filemanage.factory.ChannelFactory"%>
    <% request.setCharacterEncoding("GBK"); 
    String channelId = "";
    String title = "";
    String news_type = "";
    String pic_file = "";
    String author = "";
    String content_summ = "";
    String content = "";
    String ispinglun = "";
    String option = "";String picPath = getServletContext().getRealPath("/")+"UserFiles/Image/";
    String filePath = getServletContext().getRealPath("/")+"UserFiles/File/";
    String tmpDirectory = getServletContext().getRealPath("/")+"tmp/";  // 申明临时目录
    int maxPostSize = 4 * 1024 * 1024;    // 申明限制上传文件总大小为, 单位为 byte, -1 表示无限制  
    DecimalFormat df = new DecimalFormat("00");
    int m = 0;
    String id = UUIDHexGenerator.getUUIDHex();
    INewsFilesDAO newsFile = NewsFilesFactory.newInstance();   try{
            DiskFileUpload fu = new DiskFileUpload();
            fu.setSizeMax(maxPostSize);                 //设置文件大小.        fu.setSizeThreshold(4096);              //设置缓冲大小.        fu.setRepositoryPath(tmpDirectory);      //设置临时目录.
      
      List fileItems = fu.parseRequest(request);   //解析请求,返回一个集合.
            
            Iterator i = fileItems.iterator();        while(i.hasNext()) {
         
                FileItem fi = (FileItem)i.next();
                
       //这是用来确定是否为文件属性, 
       if(fi.isFormField()) {  //  String fieldName = fi.getFieldName();     //这里取得表单名
       if (fi.getFieldName().equals("channelId")) channelId = fi.getString("gbk");
       if (fi.getFieldName().equals("title")) title = fi.getString("gbk");
       if (fi.getFieldName().equals("news_type")) news_type = fi.getString("gbk");
       if (fi.getFieldName().equals("author")) author = fi.getString("gbk");
       if (fi.getFieldName().equals("content_summ")) content_summ = fi.getString("gbk");
       if (fi.getFieldName().equals("content")) content = fi.getString("gbk");
       if (fi.getFieldName().equals("ispinglun")) ispinglun = fi.getString("gbk");
        } else { //这里开始外理文件
                     String fileName = fi.getName();   // 返回文件名包括客户机路径
                     long size = fi.getSize();
                     if(fileName!=null && !fileName.equals("")) {
      fileName = fileName.replace('\\','/');
      String ext = fileName.substring(fileName.lastIndexOf("."),fileName.length());
          
      if (fi.getFieldName().equals("pic_file")){
           pic_file = FormatDate.getDate("yyyyMMddHHmmss") + df.format(m)+ ext;
       fi.write(new File(picPath+pic_file));     // 写文件到服务器.
      }
      //附件处理
      if(fi.getFieldName().equals("option")){
      option = FormatDate.getDate("yyyyMMddHHmmss") + df.format(m++) + ext;
      if(!ext.equals(".exe")){
        if(newsFile.insert(id,fileName,option) ){
        fi.write(new File(filePath+option));     // 写文件到服务器.
        }
      }
      }
      
           }
        }
             }
       }
      catch(Exception e){
      %>  
    <script language="JavaScript">
    alert("文件上传错误.");
    history.back();
    </script>
    <% return; 
    }
      Channel channel = ChannelFactory.newInstance().getRecord(channelId);
    News news = new News();
    news.setId(id);
    news.setChannel_id(channelId);
    news.setTitle(title);
    news.setNews_type(news_type);
    news.setAuthor(author);
    news.setIssue_state(channel.getFlag()); // 0:未审核, 1:已审核
    news.setContent_summ(content_summ);
    news.setContent(content);
    news.setIspinglun(ispinglun);
    news.setPic_file(pic_file);
    if(NewsFactory.newInstance().insert(news)){
    response.sendRedirect("NewsList.jsp?channelId="+channelId);
    } else{
    %>
    <script language="JavaScript">
    alert("信息提交失败,请重新提交");
    history.back();
    </script>
    <%}%>
      

  2.   

    //  String fieldName = fi.getFieldName();    //这里取得表单名 
      if (fi.getFieldName().equals("channelId")) channelId = fi.getString("gbk"); 
      if (fi.getFieldName().equals("title")) title = fi.getString("gbk"); 
      if (fi.getFieldName().equals("news_type")) news_type = fi.getString("gbk"); 
      if (fi.getFieldName().equals("author")) author = fi.getString("gbk"); 
      if (fi.getFieldName().equals("content_summ")) content_summ = fi.getString("gbk"); 
      if (fi.getFieldName().equals("content")) content = fi.getString("gbk"); 
      if (fi.getFieldName().equals("ispinglun")) ispinglun = fi.getString("gbk"); 
    楼上这位仁兄的做法我是知道了,可是我是想有没更好的做法呢???
    希望高手能提供点意见???
      

  3.   

    目前还没发现更好的方法。以前用过jspupload,但有一段时间这个东东下载不下来。使用起来相对简单一下,但上传稍大一点的文件,资源占用很严重,与是放弃,转而回到commons-fileupload.jar
      

  4.   

    应该是你上传的文件太大了,导致表单数据提交不上去了吧,struts2.0有个struts.properties可以配置下就好了,struts1.0的我不是很清楚怎么解决
      

  5.   

    看来你是一个比较完美的程序员,那你用jspupload吧,这个不用循环,就像接收request.getParameter()一样方便。
      

  6.   


    是啊,既然做了就要做到最好!不是么?jspupload这两天刚在研究