jquery ajax--struts2上传图片

 

//这是html标签

<form action="address/fileUpload.do" id="form2" name="form2" encType="multipart/form-data"  method="post" target="hidden_frame_1" >  

    <input type="file" id="myFile" name="myFile" style="width:450px">  

    <input type="text" id="fileName" name="fileName" /> 

    <INPUT type="submit" value="上传文件"><span id="msg"></span>  

    <br>   

    <font color="red">支持JPG,JPEG,GIF,BMP,SWF,RMVB,RM,AVI文件的上传</font>              

    <iframe name='hidden_frame_1' id="hidden_frame_1" style='display:none' ></br></iframe>  

</form> 

 

//以下是struts2标签

<s:form target="ifr_up" id="uploadImageForm" name="uploadImageForm" enctype="multipart/form-data" method="post" action="/address/fileUpload.do" >

<input type="file" name="myFile" id="myFile" style="outline: medium none; width: 75px; height: 25px; visibility: visible; " hidefoucs="true" 

 οnchange="uploadImage()" />

</s:form>

 

<iframe id="ifr_up" name="ifr_up" style="display: none;"></iframe>

 

js

/**

 * 回调函数

 */

function callback(data){

 

if(data.msg=="true"){

alert("发布成功");

}else{

alert("发布失败");

}

}

//后台

//action

private File myFile; //对应form表单的file name的名字myFile

private String fileName; //对应form表单的file name的名字fileName,这一部是为什么呢,因为myFile传过去的是一个以.tmp结尾的文件,是一个临时的,所以到时获取不到文件的后缀了,所以弄一个隐藏域文件框带到后台来获取文件的名字

 

 

public File getMyFile() {

return myFile;

}

 

public void setMyFile(File myFile) {

this.myFile = myFile;

}

 

 

public String getFileName() {

return fileName;

}

 

public void setFileName(String fileName) {

this.fileName = fileName;

}

 

 

public String fileUpload() throws Exception {

log.info("开始上传图标........");

  HttpServletRequest  request  = ServletActionContext.getRequest();   //请求的值

 String filePath=ServletActionContext.getServletContext().getInitParameter("filePath");

 

 

 if(StringUtils.isBlank(filePath)){

        log.info("filePath为空");

        filePath="images/tempUploadFile";

         

         }

 

 

 

long fileSize=myFile.length();

             if(fileSize>(1024*1024)){  //1mb文件大小

             

           

            ServletActionContext.getResponse().getWriter().write("<script>alert('文件超过指定的大小')</script>"); 

             }else{

            //String imageFileName = new Date().getTime() +getExtention(fileName); //图片的名字

             

            File imageFile = new File(ServletActionContext.getServletContext().getRealPath(filePath ) + "/" + fileName.substring(fileName.lastIndexOf('\\')+1));   //文件临时储存地

             

            boolean flag= copy(myFile, imageFile);  

if(flag){

String rspBody = "{\"msg\":\"" + true + "\"}";

ServletActionContext.getResponse().getWriter().write("<script>alert('上传成功')</script>"); ServletActionContext.getResponse().getWriter().write("<script>parent.callback(\""+rspBody+"\")</script>");//调用js的代码

}

}

 

}

private static boolean copy(File src, File dst)  {  

   try  {  

    int BUFFER_SIZE = 16 * 1024 ;  

      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)  {  

    return false; 

  }   

   return true;

}

//获取扩展名

private static String getExtention(String fileName)  {  

         int pos = fileName.lastIndexOf( "." );  

         return fileName.substring(pos);  

    }   

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值