CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  Web 开发

如何获取file控件的值!(在线等待)

楼主centerlife()2002-06-19 09:01:50 在 Java / Web 开发 提问

使用file控件时,但是没有点取该控件时,此时该控件的值等于什么呢? 问题点数:20、回复次数:7Top

1 楼qiming_fawcom(卧底神探)回复于 2002-06-19 09:11:31 得分 10

import     java.io.*;      
  import     javax.servlet.http.HttpServletRequest;      
  import     javax.servlet.ServletInputStream;      
  import     javax.servlet.ServletException;      
   
  public     class     upload  
  {      
  private     static     String     newline     =     "\n";      
  private     String     uploadDirectory     =     ".";      
  private     String     ContentType     =     "";      
  private     String     CharacterEncoding     =     "";      
  String   file1="";  
  String   ext="";  
   
  private     String     getFileName(String     s)  
  {      
  int     i     =     s.lastIndexOf("\\");      
  if(i     <     0     ||     i     >=     s.length()     -     1)  
  {      
  i     =     s.lastIndexOf("/");      
  if(i     <     0     ||     i     >=     s.length()     -     1)      
  return     s;      
  }      
  return     s.substring(i     +     1);      
  }      
   
  public     void     setUploadDirectory(String     s)  
  {      
  uploadDirectory     =     s;      
  }      
   
  public     void     setContentType(String     s)  
  {      
  ContentType     =     s;      
  int     j;      
  if((j     =     ContentType.indexOf("boundary="))     !=     -1)  
  {      
  ContentType     =     ContentType.substring(j     +     9);      
  ContentType     =     "--"     +     ContentType;      
  }      
  }      
   
  public     void     setCharacterEncoding(String     s)  
  {      
  CharacterEncoding     =     s;      
  }      
  public   void   getfile(String   filename01)    
  {  
  file1=filename01;  
       
  }    
   
  public     String     uploadFile(HttpServletRequest     req)   throws     ServletException,     IOException  
  {      
  setCharacterEncoding(req.getCharacterEncoding());      
  setContentType(req.getContentType());      
  String   filename01=uploadFile(req.getInputStream());  
  return   filename01;      
  }      
   
  public     String     uploadFile(ServletInputStream     servletinputstream)      
  throws     ServletException,     IOException  
  {      
  String     filename     =     null;      
  byte     Linebyte[]     =     new     byte[4096];      
  byte     outLinebyte[]     =     new     byte[4096];      
  int     ai[]     =     new     int[1];      
  String     line;      
  //得到文件名      
  while((line     =     readLine//Reads   the   input   stream,   one   line   at   a   time.    
  (Linebyte,     ai,     servletinputstream,     CharacterEncoding))     !=     null)  
  {      
  int     i     =     line.indexOf("filename=");      
  if(i     >=     0){      
  line     =     line.substring(i     +     10);      
  if((i     =     line.indexOf("\""))     >     0)      
  line     =     line.substring(0,     i);      
  break;      
  }      
  }      
    filename     =     line;      
          if(filename     !=     null     &&     !filename.equals("\""))  
          {      
  filename     =     getFileName(filename);  
  file1=file1+filename.substring(filename.indexOf("."));  
     
     
  String     sContentType     =     readLine(Linebyte,ai,servletinputstream,CharacterEncoding);      
  if(sContentType.indexOf("Content-Type")     >=     0)      
  readLine(Linebyte,     ai,     servletinputstream,     CharacterEncoding);      
  //File(String     parent,     String     child)      
  //Creates     a     new     File     instance     from     a     parent     pathname     string      
  //and     a     child     pathname     string.      
  File     file     =     new   File(uploadDirectory,file1);      
  //FileOutputStream(File     file)      
  //Creates     a     file     output     stream     to     write     to     the     file     represented      
  //by     the     specified     File     object.      
  FileOutputStream     fileoutputstream     =     new     FileOutputStream(file);      
  while((sContentType   =   readLine(Linebyte,ai,servletinputstream,CharacterEncoding))     !=     null)  
  {      
        if(sContentType.indexOf(ContentType)     ==     0     &&     Linebyte[0]     ==     45)      
  break;      
       
        //write(byte[]     b,     int     off,     int     len)      
                        //Writes     len     bytes     from     the     specified     byte     array     starting      
                        //at     offset     off     to     this     file     output     stream.      
        fileoutputstream.write(Linebyte,     0,     ai[0]);      
        fileoutputstream.flush();      
       
              }      
           
              fileoutputstream.close();      
      }      
      return   filename;  
  }      
   
  private   String   readLine(byte   Linebyte[],int   ai[],ServletInputStream     servletinputstream,String   CharacterEncoding)  
  {      
  try  
  {      
  //readLine(byte[]     buffer,     int     offset,     int     length)      
  //Reads     a     line     from     the     POST     data.      
                  ai[0]     =     servletinputstream.readLine(Linebyte,     0,     Linebyte.length);      
  if(ai[0]     ==     -1)      
  return     null;      
  }catch(IOException     _ex){      
  return     null;      
  }      
  try{      
  if(CharacterEncoding     ==     null){      
  //用缺省的编码方式把给定的byte数组转换为字符串      
  //String(byte[]     bytes,     int     offset,     int     length)      
  return     new     String(Linebyte,     0,     ai[0]);      
  }  
  else{      
  //用给定的编码方式把给定的byte数组转换为字符串      
  //String(byte[]     bytes,     int     offset,     int     length,     String     enc)      
  return     new     String(Linebyte,     0,     ai[0],     CharacterEncoding);      
  }      
  }  
  catch(Exception     _ex){      
  return     null;      
  }      
  }      
   
  }      
  Top

2 楼xycleo()虚竹和尚()回复于 2002-06-19 09:12:08 得分 0

request.getParameter("file");Top

3 楼centerlife()回复于 2002-06-19 09:42:20 得分 0

通过另外一种途径解决了这个问题啦!  
  谢谢各位。  
  我原来只是想如何判断该控件为空值,可以通过字符串的长度或者判断“."的位置来确定。  
  现在有一个问题就是如何通过该控件来获取文件名,不要带路径!Top

4 楼weidegong(weidegong)回复于 2002-06-19 09:47:24 得分 10

<input   type=file   name=show>  
  <input   type=button   value=get   onclick=get()>  
  <script>  
  function   get(){  
  var   str=document.all("show").value;  
  var   arr=str.split('\\');  
  alert(arr[arr.length-1]);  
  }  
  </script>Top

5 楼centerlife()回复于 2002-06-19 09:53:13 得分 0

不用javascript,只用java,如何转换啊?我就是找不到相关的函数!Top

6 楼weidegong(weidegong)回复于 2002-06-19 10:04:21 得分 0

你用什么上传的?如果用smartUpload很容易得到文件名啊?  
   
      import   java.util.regex.*;  
  String   str="A|B|C|D";  
  Pattern   patten=Pattern.compile("|");  
  String[]   result   =   patten.split(str);Top

7 楼centerlife()回复于 2002-06-19 10:42:56 得分 0

已经结贴啦!但当时提交时出现错误。分已经给了。Top

相关问题

  • asp.net 获取 HTML控件值
  • 获取服务器控件值,急等
  • 如何获取自定义控件中子控件的值?
  • 如何获取web用户控件里面控件的值
  • 怎样给Edit控件赋值,还有获取输入的值?
  • 怎样获取当前所选数组控件的下标值?
  • 如何获取所单击web控件的坐标值?
  • DBGrid控件列值最大宽度的获取。
  • 怎么获取用户控件内的值?
  • JavaScript如何获取自定义控件中的TextBox1的值?

关键词

  • 控件
  • 文件名
  • ai
  • servlet
  • null
  • servletinputstream
  • uploaddirectory
  • linebyte
  • characterencoding
  • contenttype

得分解答快速导航

  • 帖主:centerlife
  • qiming_fawcom
  • weidegong

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo