| 
 | 
 
| 
 
 1,文件上传 
 
1)struts.xml配置 
 
<action name="addStockEx" class="action.stock.AddStockAction"> 
         <param name="savePath">/images/upload</param> <!--这里说明保存的目录--> 
         <param name="allowTypes">image/bmp,image/png,image/gif,image/jpg,image/jpeg</param> <!--允许上传的文件类型--> 
      <result name="SUCC">/jsp/success.jsp</result> <!--返回成功--> 
      <result name="input">/jsp/upload.jsp</result>        <!--上传错误返回页面--> 
   </action> 
 
2)Action类 
 
public class UploadFile 
 
{  
 
  private String allowTypes; 
private String savePath; 
private File upload; 
private String uploadContentType; 
private String uploadFileName;//注意这里是***FileName,其中***是File的变量名 
 
public String execute() throws Exception 
 
{ 
 
   boolean b = filterType(getAllowTypes().split("\\,")); 
  if(!b) 
  { 
   setTip("添加失败!"); 
   setError("不支持此类型的文件,请传入.bmp .png .gif .jpg .jpeg格式的文件!"); 
   return "input"; 
  } 
   
  //file upload 
  FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+getUploadFileName()); 
  FileInputStream fis = new FileInputStream(getUpload()); 
  byte[] buf = new byte[1024]; 
  int len = 0; 
  while ((len=fis.read(buf))>0) 
  { 
   fos.write(buf,0,len); 
  } 
 
} 
 
//判断上传文件类型是否支持 
 
public boolean filterType(String[] types) 
{ 
  boolean b = false; 
  String fileType = getUploadContentType(); 
  for(String type : types) 
  { 
   if(type.equals(fileType)) 
   { 
    b = true; 
   } 
  } 
   
  return b; 
} 
 
//.....这里省略get  set方法 
 
}  
 
3),jsp页面 
 
<form name="theForm" method="post" enctype="MULTIPART/FORM-DATA"> 
 
  <input type="file"name="upload"> 
 
</form> 
 
2,文件下载 
 
1)struts.xml配置 
 
<action name="download" class="action.FileDownLoadAction"> 
      <param name="inputPath">/images/button_18.gif</param><!--下载的文件--> 
      <result name="SUCC" type="stream"> 
       
      <param name="contentType">/image/gif</param><!--下载文件的类型--> 
      <param name="inputName">targetFile</param><!--注意这里,在action类中肯定会有个getTargetFile()方法--> 
      <param name="contentDisposition">attachment;filename="hah.gif"</param>  <!--下载文件的名称,注意这里需要定义attachment;  否则就会默认inline 表示浏览器会尝试打开--> 
      <param name="bufferSize">4096</param>  <!--下载缓冲区大小--> 
      </result> 
    </action> 
 
2)Action类 
 
public class FileDownLoadAction  
{ 
private String inputPath; 
 
 
public InputStream getTargetFile() { 
  return ServletActionContext.getServletContext().getResourceAsStream(inputPath); 
} 
 
public void setInputPath(String inputPath) { 
  this.inputPath = inputPath; 
} 
public String execute() 
{ 
  return "SUCC"; 
} 
 
public String getInputPath() { 
  return inputPath; 
} 
 
 
} 
 
3)jsp页面 
 
<a href="download.action">下载</a> 
 
 
 
一些参数说明: 
 
contentType  
 
内容类型,和互联网MIME标准中的规定类型一致,例如text/plain代表纯文本,text/xml表示XML,image/gif代表GIF图片,image/jpeg代表JPG图片  
 
inputName  
 
下载文件的来源流,对应着action类中某个类型为Inputstream的属性名,例如取值为inputStream的属性需要编写getInputStream()方法  
 
contentDisposition  
 
文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。取值为:  
 
attachment;filename="hah.gif",表示文件下载的时候保存的名字应为hah.gif。如果直接写filename="hah.gif",那么默认情况是代表inline,浏览器会尝试自动打开它,等价于这样的写法:inline; filename="hah.gif"  
 
bufferSize  
 
下载缓冲区的大小  
 
。在这里面,contentType属性和contentDisposition分别对应着HTTP响应中的头Content-Type和Content-disposition头。 |   
 
 
 
 |