---
title: "web open file -fileupload -validator"
author: "microsoft_hang"
date: "2009-06-24T18:51:42Z"
tags: []
slug: "web-open-file-fileupload-validator"
draft: false
lastmod: "2009-06-24T18:51:42Z"
id: 414371
url: "https://microsoft-hang.iteye.com/blog/414371"
---

最近在项目中用到不少小东西,希望对大家有帮助

首先:

比如说在web页面打开一个应用程序 (txt doc)

 

B/S

&lt;a href="[http://&lt;%=request.getLocalAddr()%&gt;:&lt;%=request.getLocalPort()%&gt;&lt;%=request.getContextPath()%&gt;/UploadFile/](http://%3c%=request.getlocaladdr%28%29%%3e:%3C%=request.getLocalPort%28%29%%3E%3C%=request.getContextPath%28%29%%3E/UploadFile/)文件" target="\_blank"&gt;click&lt;/a&gt;

假如不能成功则在web.xml配置

  &lt;mime-mapping&gt; （txt配置）  
    &lt;extension&gt;txt&lt;/extension&gt;  
    &lt;mime-type&gt;text/plain&lt;/mime-type&gt;  
  &lt;/mime-mapping&gt;

txt请注意文件的编码集 utf-8 否则乱码

C/S

Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler file:文件");

 

文件上传

 

在form里讲上传文件那定义为FormFile

在页面里设置enctype="multipart/form-data"

上传的控件用的是 struts标签 html:file

FormFile file = Form.getFormFile()；取得上传的文件

String fileName=file.getFileName();

InputStream stream=file.getInputStream();  
   String filePath = request.getRealPath("/UploadFile");//上传到指定的UploadFile包中  
   ByteArrayOutputStream baos = new ByteArrayOutputStream();  
   fileName=CurrentTimes.getCurrentTime()+file.getFileName().substring(file.getFileName().lastIndexOf("."), file.getFileName().length());  
   OutputStream bos = new FileOutputStream(filePath + "/" + fileName);//建立一个上传文件的输出流     
   int bytesRead = 0;  
   byte\[] buffer = new byte\[8192];  
   while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1)  
   {  
    bos.write(buffer, 0, bytesRead);    
   }  
   bos.close();  
   stream.close(); 

还有用jspsmartupload.jar 上传也十分方便但是pageContext对象在aciton中无法得到 故此放弃

 

JQuery验证框架validator

 

将资源导入文件，然后在form提交处写入onsubmit="return Validator.Validate(this,3)"

在需要验证的页面元素中 设置 dataType="验证类型" msg="提示消息" 具体类型以及使用请自行下载帮助

使用起来非常的方便

&lt;!-- --&gt;