zoukankan      html  css  js  c++  java
  • Struts2 使用Jquery+ajax 文件上传

    话不多说 直接上代码

    前台js:

     1 var formData = new FormData();
     2 formData.append("file1",$("#file1")[0].files[0]);//第一个file1代表后台文件属性名,第二个file1表示html中input的id
     3 $.ajax({
     4     type:"post",
     5     url:"ajax/uploadFile",
     6     data:formData,
     7     processData : false, 
     8     contentType : false,
     9     success : function(res) { 
    10         console.log(res);
    11         if(res=="success"){
    12             $("#labResult").text("文件上传成功");
    13         }
    14     }, 
    15     error : function(msg) { 
    16         console.log(msg.responsText);
    17     } 
    18 });

    后台java:

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 import java.io.InputStream;
     6 import java.io.OutputStream;
     7 
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 
    11 import org.apache.struts2.ServletActionContext;
    12 
    13 import com.opensymphony.xwork2.ActionContext;
    14 import com.opensymphony.xwork2.ActionSupport;
    15 
    16 public class UploadAction extends ActionSupport{
    17     private File file1 ; 
    18     private String file1FileName ;//FileName为固定写法
    19     public File getFile1() {
    20         return file1;
    21     }
    22     public void setFile1(File file1) {
    23         this.file1 = file1;
    24     }
    25     public String getFile1FileName() {
    26         return file1FileName;
    27     }
    28     public void setFile1FileName(String file1FileName) {
    29         this.file1FileName = file1FileName;
    30     } 
    31     public String uploadFile() throws Exception {  
    32         
    33         String path = ServletActionContext.getRequest().getServletContext().getRealPath("/WEB-INF/upload");  
    34         //查看是否存在目录 不存在则创建
    35         File file = new File(path);
    36         //如果目录不存在
    37         if(!file.exists()){
    38             file.mkdirs();
    39         }
    40         //文件不为空 则上传
    41         if(file1!=null){
    42             //输出流  
    43             OutputStream os = new FileOutputStream(new File(path,file1FileName));  
    44             //输入流  
    45             InputStream is = new FileInputStream(file1);  
    46               
    47             byte[] buf = new byte[1024];  
    48             int length = 0 ;  
    49               
    50             while(-1 != (length = is.read(buf) ) )  
    51             {  
    52                 os.write(buf, 0, length) ;  
    53             }  
    54             is.close();  
    55             os.close();  
    56         }  
    57         return "success";  
    58     }  
    59 
    60     public void uploadFile(){
    61         System.out.println("file1:"+file1+".");
    62         System.out.println("file1FileName:"+file1FileName+".");
    63         String path="";
    64         try {
    65             path=uploadFile();
    66             HttpServletResponse response = ServletActionContext.getResponse();
    67             response.setContentType("text/html;charset=utf-8");
    68             response.getWriter().write(path);
    69         } catch (Exception e) {
    70             e.printStackTrace();
    71         }
    72     }
    73 }

    struts:

    <package name="ajax" extends="json-default" namespace="/ajax">
        <action name="uploadFile" class="包名.UploadAction" method="uploadFile"></action>
    </package>

    有啥不懂,底下留言。

  • 相关阅读:
    [Maven实战-许晓斌]-[第二章]-2.2基于UNIX系统安装maven
    [Maven实战-许晓斌]-[第二章]-2.1在Windows上安装maven
    【sonar-block】Use try-with-resources or close this "BufferedInputStream" in a "finally" clause.
    sonar阻断级别错误(block)简单汇总
    让子类使用父类的Logger
    集合的addAll方法--list.addAll(null)会报错--java.lang.NullPointerException
    nice
    ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded
    Ubuntu 系统修改root密码后,无需密码亦可登录
    MySQL 查找今年的数据
  • 原文地址:https://www.cnblogs.com/jinghun/p/7535041.html
Copyright © 2011-2022 走看看