struts 2.0 图片上传

4 篇文章 0 订阅
package com.aochuang.news.action;

import com.opensymphony.xwork2.ActionSupport;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;


public class UploadImageAction extends ActionSupport {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    
    


        private static final int BUFFER_SIZE = 16 * 1024 ;
        
        

        
        
        
        
        private File myFile;         //网页中file字段的name
        private String contentType;  //struts2中必须字段,主要是set方法
        private String fileName;     // struts2中必须字段,主要是set方法
        private String imageFileName;   //要上传的图片在服务器中的名称
        private String imagePath;       //保存服务器中的路径
        private String pagePath;        //页面中要引用的url
        
        
        public File getMyFile() {
            return myFile;
        }
        public void setMyFile(File myFile) {
            this.myFile = myFile;
        }
        public String getContentType() {
            return contentType;
        }
        public void setContentType(String contentType) {
            this.contentType = contentType;
        }
        public String getFileName() {
            return fileName;
        }
        public void setFileName(String fileName) {
            this.fileName = fileName;
        }
        public String getImageFileName() {
            return imageFileName;
        }
        public void setImageFileName(String imageFileName) {
            this.imageFileName = imageFileName;
        }
        public String getImagePath() {
            return imagePath;
        }
        public void setImagePath(String imagePath) {
            this.imagePath = imagePath;
        }
        public String getPagePath() {
            return pagePath;
        }
        public void setPagePath(String pagePath) {
            this.pagePath = pagePath;
        }
        public static long getSerialVersionUID() {
            return serialVersionUID;
        }
        public static int getBUFFER_SIZE() {
            return BUFFER_SIZE;
        }

           private static void copy(File src, File dst)  {
                try  {
                   InputStream in = null ;
                   OutputStream out = null ;
                    try  {                
                       in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
                       out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);

                        byte [] buffer = new byte [BUFFER_SIZE];
                        while (in.read(buffer) > 0 )  {
                           out.write(buffer);
                       }
                    } finally  {
                        if ( null != in)  {
                           in.close();
                       }
                         if ( null != out)  {
                           out.close();
                       }
                   }
                } catch (Exception e)  {

                   e.printStackTrace();
               }
           }



            public String execute(){   
               imageFileName = new Date().getTime() + fileName.substring(fileName.lastIndexOf("\\")+2,fileName.length());   //此名称用当前时间与上传图片名组成
               imagePath = ServletActionContext.getServletContext().getRealPath( "/images/Image" ) + "\\" + imageFileName;
               System.out.println(imagePath);
               
               pagePath = getWebPath()+"images/Image" + "/" + imageFileName; //页面引用位置
               System.out.println(pagePath);
               
               File imageFile = new File(imagePath);
               copy(myFile, imageFile);
               
               
                return SUCCESS;
           }
           
           
           private String getWebPath(){
               HttpServletRequest request = ServletActionContext.getRequest ();
               String path = request.getContextPath();
               String basePath = request.getScheme() + "://"
                      + request.getServerName() + ":" + request.getServerPort()
                      + path + "/";
               return basePath;
            }

    
    

}






<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<html>
    <head>

        <title>upload</title>


        <script type="text/javascript" src="js/jquery-1.5.2.js"></script>
    
    <script type="text/javascript" src="js/jquery.js"></script>
    
    </head>

    <body>
        <s:form id="imagefile" action="uploadimage" namespace="/" method="POST" name="pos"
            enctype="multipart/form-data" >
            <table width="80%" border="1" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="20%" align="right">
                        <font color="red">*</font>上传图片文件
                    </td>
                    <td width="20%">
                        <s:file name="myFile" id="myFile"></s:file>
                    </td>
                    <td colspan="2" align="center">
                        <input type="button" οnclick="getFileName()" value="上传" />
                    </td>
                </tr>
            </table>
            <input type="hidden" name="fileName" id="fname">
        </s:form>

    </body>

    <script type="text/javascript">

    
function getFileName(){
var filesName=document.getElementById("myFile").value;
document.getElementById("fname").value=filesName;
    
    document.forms[0].submit();
        //$.post("/CKeditor/ajax/fileUploads.action",{},function(){
            //if(null!=filesName  && ""!=filesName){
                   window.returnValue=filesName;
                   //window.close();
                //}
        //},"json");
        //alert($("#myFile").val());
}
 







  </script>

</html>











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值