)属性后,即, 在处理页中,用requset.getParameter(\"xxx\"); 得到的结果是null?去掉那个便正常了呢? (我希望在一个表单里上传图片到一个目录,上传图片名到数据库) screen.width-333)this.width=scree" />
织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>JAVA>

为什么表单中加了这个(ENCTYPE=\"multipart/form-data\">)会出

罗索客 发布于 2003-03-27 14:37 点击:次 
为什么一个表单中加了这个(ENCTYPE=\"multipart/form-data\">)属性后,即, 在处理页中,用requset.getParameter(\"xxx\"); 得到的结果是null?去掉那个便正常了呢? (我希望在一个表单里上传图片到一个目录,上传图片名到数据库) screen.width-333)this.width=scree
TAG:

为什么一个表单中加了这个(ENCTYPE="multipart/form-data">)属性后,即,

在处理页中,用requset.getParameter("xxx");
得到的结果是null?去掉那个便正常了呢?

(我希望在一个表单里上传图片到一个目录,上传图片名到数据库)


新窗口浏览

Details refer to: http://www.onjava.com/pub/a/onjava/2001/04/05/upload.html?page=3

Listing 1: The code for Brainy FileUpload Bean

package com.brainysoftware.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import java.util.Dictionary;
import java.util.Hashtable;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileUploadBean {

  private String savePath, filepath, filename, contentType;
  private Dictionary fields;

  public String getFilename() {
    return filename;
  }

  public String getFilepath() {
    return filepath;
  }

  public void setSavePath(String savePath) {
    this.savePath = savePath;
  }

  public String getContentType() {
    return contentType;
  }

  public String getFieldvalue(String fieldName) {
    if (fields == null || fieldName == null)
      return null;
    return (String) fields.get(fieldName);
  }

  private void setFilename(String s) {
    if (s==null)
      return;

    int pos = s.indexOf("filename=\\"");
    if (pos != -1) {
      filepath = s.substring(pos+10, s.length()-1);
      // Windows browsers include the full path on the client
      // But Linux/Unix and Mac browsers only send the filename
      // test if this is from a Windows browser
      pos = filepath.lastIndexOf("\\\\");
      if (pos != -1)
        filename = filepath.substring(pos + 1);
      else
        filename = filepath;
    }
  }
private void setContentType(String s) {
    if (s==null)
      return;

    int pos = s.indexOf(": ");
    if (pos != -1)
      contentType = s.substring(pos+2, s.length());
  }

  public void doUpload(HttpServletRequest request) throws IOException {
    ServletInputStream in = request.getInputStream();

    byte[] line = new byte[128];
    int i = in.readLine(line, 0, 128);
    if (i < 3)
      return;
    int boundaryLength = i - 2;

    String boundary = new String(line, 0, boundaryLength); //-2 discards the newline character
    fields = new Hashtable();

    while (i != -1) {
      String newLine = new String(line, 0, i);
      if (newLine.startsWith("Content-Disposition: form-data; name=\\"")) {
        if (newLine.indexOf("filename=\\"") != -1) {
          setFilename(new String(line, 0, i-2));
          if (filename==null)
            return;
          //this is the file content
          i = in.readLine(line, 0, 128);
          setContentType(new String(line, 0, i-2));
          i = in.readLine(line, 0, 128);
          // blank line
          i = in.readLine(line, 0, 128);
          newLine = new String(line, 0, i);
          PrintWriter pw = new PrintWriter(new BufferedWriter(new
            FileWriter((savePath==null? "" : savePath) + filename)));
          while (i != -1 && !newLine.startsWith(boundary)) {
            // the problem is the last line of the file content
            // contains the new line character.
            // So, we need to check if the current line is
            // the last line.
            i = in.readLine(line, 0, 128);
            if ((i==boundaryLength+2 || i==boundaryLength+4) // + 4 is eof
              && (new String(line, 0, i).startsWith(boundary)))
              pw.print(newLine.substring(0, newLine.length()-2));
            else
              pw.print(newLine);
            newLine = new String(line, 0, i);

          }
          pw.close();

        }
        else {
          //this is a field
          // get the field name
          int pos = newLine.indexOf("name=\\"");
          String fieldName = newLine.substring(pos+6, newLine.length()-3);
          //System.out.println("fieldName:" + fieldName);
          // blank line
          i = in.readLine(line, 0, 128);
          i = in.readLine(line, 0, 128);
          newLine = new String(line, 0, i);
          StringBuffer fieldvalue = new StringBuffer(128);
          while (i != -1 && !newLine.startsWith(boundary)) {
            // The last line of the field
            // contains the new line character.
            // So, we need to check if the current line is
            // the last line.
            i = in.readLine(line, 0, 128);
            if ((i==boundaryLength+2 || i==boundaryLength+4) // + 4 is eof
              && (new String(line, 0, i).startsWith(boundary)))
              fieldvalue.append(newLine.substring(0, newLine.length()-2));
            else
              fieldvalue.append(newLine);
            newLine = new String(line, 0, i);
          }
          //System.out.println("fieldvalue:" + fieldvalue.toString());
          fields.put(fieldName, fieldvalue.toString());
        }
      }
      i = in.readLine(line, 0, 128);

    } // end while
  }
}     (iwgh)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/200303/1360.html]
本文出处: 作者:iwgh
顶一下
(1)
25%
踩一下
(3)
75%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容