[Java基础]-- struts2实现单文件或者多文件上传和单文件下载
    一、struts2实现上传文件
1、单文件上传
   (1)java代码
private File  uploadImage;                                         //jsp中的name属性
private String uploadImageContentType;             //文件类型严格要求=uploadImage+ContentType
private String uploadImageFileName;                  //获取文件名称严格要求=uploadImage+FileName
//给以上属性提供get、set方法
public String upload() throws Exception{
 String realPath = "E:/temp/";
 File file = new File(realPath);//判断是否有该文件夹,如果没有就新建一个文件夹
 if(!file.exists()){
 file.mkdirs();}
System.out.println("上传的文件名称:"+uploadImageFileName);
//将文件处理到指定文件夹下
 FileUtils.copyFile(uploadImage, new File(realPath+uploadImageFileName));
 return "uploadSuccess";
 }    (2)struts.xml配置文件
<action name="UploadAction_upload" method="upload" class="com.action.UploadAction">
 <result name="uploadSuccess">/uploadOk.jsp</result>
 <result name="uploadFail">/uploadError.jsp</result>
 </action>    (3)jsp页面
  <form enctype="multipart/form-data" action="${pageContext.request.contextPath }/UploadAction_upload" method="post">
     <input type="file" name="uploadImage"/>
     <input type="submit" value="提交"/>
     </form>2、多文件上传
   (1)java代码
private File[] uploadImages;                               //jsp中name属性对应的名称   
 private String[] uploadImagesContentType;   //固定要求=uploadImages+ContentType   上传的文件类型集合
 private String[] uploadImagesFileName;        //固定要求=uploadImages+FileName        上传的文件名称集合//提供get、set方法
public String upload() throws Exception{
 String realPath = "E:/temp/";
 File file = new File(realPath);
 if(!file.exists()){
 file.mkdirs();}
//遍历集合
 for(int i=0;i<uploadImages.length;i++){
 File uploadImage = uploadImages[i];
 FileUtils.copyFile(uploadImage,new File(file,uploadImagesFileName[i]));
 }
 return "uploadImagesSuccess";
 }  (2)struts.xml代码
<action name="UploadImagesAction_upload" method="upload" class="com.action.UploadImagesAction">
 <result name="uploadImagesSuccess">/upImagesOk.jsp</result>
 </action>    (3)jsp页面
    <form enctype="multipart/form-data" action="${pageContext.request.contextPath }/test/UploadImagesAction_upload" method="post">
     <input type="file" name="uploadImages"/><br/>
     <input type="file" name="uploadImages"/><br/>
     <input type="file" name="uploadImages"/><br/>
     <input type="submit" value="提交"/>
     </form>二、strtuts2实现下载文件
1、下载文件
(1)java代码
private InputStream fileStream;             //解析流的名称,对应struts.xml文件里的param标签中的name="inputStream"的值
private String uploadImageFileName;//传递的下载文件名。如:123.txt
//提供get、set方法
public String download() throws Exception{
 try {
 StringBuffer path=new StringBuffer("F:/phtoto.jpg");
 System.out.println("图片的真实路径:"+path);
fileStream=new FileInputStream(path.toString());
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 }
 return "downloadSuccess";
 }      (2)struts.xml文件配置
<action name="DownloadAction_download" method="download" class="com.action.DownloadAction">
 <result name="downloadSuccess" type="stream">
  <param name="contentType">image/jpeg</param>  
 
    
 
                 <param name="inputName">
 fileStream</param>  
 
      
 
                  <param name="contentDisposition">
 attachment;filename=
 "${uploadImageeeFileName}"</param><!--取值的方式-->
   </result>
</action>   
         (3)jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 <%@ taglib uri="/struts-tags" prefix="s" %>
 <%String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 %>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
   <head>
     <base href="<%=basePath%>">
     <title>online</title>
     
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
   </head>
   <body>  
    <!--123.jpg代表下载后的文件名称,action中的photo.jpg是下载的源文件名称--><a href="${pageContext.request.contextPath}DownloadAction_download?uploadImageFileName=123.jpg" >下载</a>  
   </body>
 </html>2、在线显示文件
(1)java代码
private FileInputStream fileStream;    //作为流给struts.xml解析
private String uploadImageFileName;//获取文件名
//提供get和set方法
//在线显示
 public String onlinePhoto(){
 try {
 FileInputStream file=new FileInputStream(new File("E:/temp/"+uploadImageFileName));
 fileStream=file;
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 }
 return "onlineOk";
 }(2)struts.xml文件配置
<action name="UploadAction_upload" method="upload" class="com.action.UploadAction">
 <result name="onlineOk" type="stream">
 <!--设置解析的格式 -->
 <param name="contentType">image/jpeg</param>
 <!-- 得到参数流 -->
 <param name="inputName">fileStream</param>
 <param name="contentDisposition">inline</param>
 <!--缓冲大小 -->
 <param name="bufferSize">1024</param>
 </result>
 </action>(3)jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 <%@ taglib uri="/struts-tags" prefix="s" %>
 <%
 String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 %>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
   <head>
     <base href="<%=basePath%>">
     <title>online</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
   </head>
   <body>  
     <img  src="${pageContext.request.contextPath}/test/UploadAction_onlinePhoto?uploadImageFileName=newName.jpg" style="width: 100px;height:120px;" />  
   </body>
 </html>