解决firefox3.0,ie8 在上传文件时只能获得文件名,而不能获得文件路径的问题

 解决firefox3.0,ie8 在上传文件时只能获得文件名,而不能获得文件路径的问题:
 
  我们的目的是要获取到文件的全路径,包括文件地址和文件名。下面我们以(struts 1.x)为例讲解
------------------------------------------------------------------------------------
 1.jsp页面
    <html:form action="importAction" method="post" enctype="multipart/form-data">
      <html:file property="file"/>
        <html:submit/>
   </html:form>
   
    注意:我们采用<html:file>标签来实现文件上传,该标签必须嵌套在<html:form>中,并且加上:enctype属性
   
          在<html:file>中property属性名file 应该和actionform中FormFile类型的属性对应。
         
         
  2. ActionForm
    public class UploadActionForm extends ActionForm {
   private FormFile file;
  
   public FormFile getFile() {
    return file;
   }
  
   public void setFile(FormFile file) {
    this.file = file;
   }
   }
 
 
  3.Action
     public  class UploadAction {
        public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
          
          //获取客户端参数
          
          UploadActionForm uaf = (UpaloadActioinForm) form;
          
          FormFile file= uaf.getFile();
          
          String fileFullAddress = copyFile(file);
           
           
            //fileFullAddress就是我们说要的文件全路径(文件路径+文件名)
           
           // 使用得到的文件全路径进行操作   
       
        }
    
       //文件拷贝,返回的字符串为文件的路径+文件名
      private String copyFile(FormFile file) throws IOException {
     InputStream in = null;
     OutputStream out = null;
     String address = "D:/newFile.xml";
     try {
      in = new BufferedInputStream(file.getInputStream());
      out = new BufferedOutputStream(new FileOutputStream(address));
      int len;
      while ((len = in.read()) != -1) {
       out.write(len);
       out.flush();
      }
     } catch (IOException e) {
      log.error("文件创建出错!");
      e.printStackTrace();
     } finally {
      if (out != null) {
       out.close();
      }
      if (in != null) {
       in.close();
      }
     }
     return address;
      }
 
 }
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值