struts2上传

关于jsp如何实现文件上传,未来将有博主将有代码贴出来,现在讨论下struts下的文件上传及下载

struts2文件上传技术原理:

1.struts2文件上传的底层支持
在struts.properties配置文件中,配置struts2上传文件时的上传解析器:
struts.multipart.parser=cos指定使用cos的文件上传解析器
struts.multipart.parser=pell指定使用pell的文件上传解析器
struts.multipart.parser=jakarta    指定使用common-fileupload文件上传解析器,默认的
 
2.每个文件上传域  需要在action中提供3个field来封装
(1)与文件上传域的name同名的,类型为File的field
(2)名为文件上传域的name+FileName,类型为string的field
(3) 名为文件上传域的name+ContentType,类型为string的field
其他参数按照普通参数提供field和构造函数

3.使用io流控制将文件写入服务器的物理磁盘即可
4.文件过滤
文件过滤的方式有两种:
a  在action通过代码进行控制。File类型的Field可以获取上传文件的类型
string类型XXXContentType可以获取上传文件的类型,通过判断上传文件大小、文件类型可对上传文件进行过滤
b 使用fileUpload拦截器,只要进行简单的配置即可
(1)启用fileUpload拦截器,当文件过滤失败时struts会返回input逻辑视图名,在struts.xml中配置action,为其指定两个参数,即
---allowedTypes,该参数指定允许上传的文件类型,多个文件类型之间用,隔开,如image/jpeg,image/pjpeg,image/gif,image/png等
---maximumSize:该参数指定允许上传的文件大小,单位是字节(1kb=1024字节)
注意:启用fileUpload拦截器之后,还要手动启动默认拦截器defaultStack:
    <interceptor-ref name="fileUpload">
<!--      maximumSize 字节大小 -->
     <param name="allowedTypes">image/png,image/jpeg,image/pjpeg,image/gif</param>
     <param name="maximumSize">20000</param>   
     </interceptor-ref>
     <interceptor-ref name="defaultStack"></interceptor-ref>

(2)需要为input逻辑视图名配置物理视图资源
 <result name="input">upload.jsp</result>

主要实现代码如下

AddPicProAction.java文件,该action实现文件的上传

package com.gree.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;

import com.gree.service.PicService;
import com.opensymphony.xwork2.ActionSupport;

public class AddPicProAction extends ActionSupport {
    
    private String picName;
         //每个文件域需要3个field封装
    private File pic;          //上传的文件
    private String picFileName; //上传文件的原始文件名
    private String  picContentType;//上传文件的文件类型
    public String getPicName() {
        return picName;
    }
    public void setPicName(String picName) {
        this.picName = picName;
    }
    public File getPic() {
        return pic;
    }
    public void setPic(File pic) {
        this.pic = pic;
    }
    public String getPicFileName() {
        return picFileName;
    }
    public void setPicFileName(String picFileName) {
        this.picFileName = picFileName;
    }
    public String getPicContentType() { 
        return picContentType;
    }
    public void setPicContentType(String picContentType) {
        this.picContentType = picContentType;
    }
    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.ActionSupport#execute()
     */
    @Override
    public String execute() throws Exception {
        //此处pic就代表了我们的文件,因此此处只要io流将文件写入磁盘即可
        String uploadpath=ServletActionContext.getServletContext().getRealPath("/upload");
//        String houzui="";
//        if(picFileName.lastIndexOf(".")!=-1) {
//         houzui =picFileName.substring(picFileName.lastIndexOf("."));  
//         }

         
        String newFileName =picFileName;
                
        
        
        
        //这代表要上传的文件,文件上传就是io流
        FileOutputStream os=new FileOutputStream(uploadpath+"/"+newFileName);
        FileInputStream is=new FileInputStream(pic);
         int hasRead=0;
         
         byte[] buff=new byte[2014];
         
         while((hasRead=is.read(buff))>0){
             
             os.write(buff,0,hasRead);                        
         }
         is.close();
         os.close();
         
         PicService ps=new PicService();
         ps.addpic(picName,newFileName);
        return SUCCESS;
        
        
        
    }
    
    
    

}

在WebContent根目录下建立一个文件夹upload,如图:

struts.xml配置如下:

  
   <action name="addPicPro" class="com.gree.action.AddPicProAction">
   
   
   
     <interceptor-ref name="fileUpload">
<!--      maximumSize 字节大小 -->
     <param name="allowedTypes">image/png,image/jpeg,image/pjpeg,image/gif</param>
     <param name="maximumSize">2000000000000</param>   
     </interceptor-ref>
     <interceptor-ref name="defaultStack"></interceptor-ref>
      <result type="chain">listpics</result>
      <result name="input">upload.jsp</result>
   </action>

jsp文件如下:

upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>My JSP 'upload.jsp' starting page</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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<s:head/>
  </head>
  
  <body>
    <s:form action="addPicPro" enctype="multipart/form-data" method="post" namespace="/">
    <s:textfield name="picName" label="图片名"/>
    <s:file name="pic" label="选择图片"/>
    <s:submit value="上传"/>
    </s:form>
    
   
    
    
    
  </body>
</html>

listPicts.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>My JSP 'listPicts.jsp' starting page</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">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    
    
    <script>
    
    function func(o){
    
    document.getElementById("show").src="upload/"+o;
    
    
    }
    
    
    
    </script>

  </head>
<!--   listValue 是文本内容     listKey是value -->
  <s:select list="picts" listKey="fileName" listValue="picName" label="请选择图片" 
  onchange="func(this.value)"/>    
  <img id="show" width=200/>
  <body>
  </body>
</html>

好了,有什么问题可回复博主,博主有时间会回答

原文地址:https://www.cnblogs.com/binggu/p/4087442.html