java Struts2框架下实现文件上传功能

网友投稿 201 2023-07-02


java Struts2框架下实现文件上传功能

本文实例为大家分享了Struts2框架实现文件上传的方法,供大家参考,具体内容如下

struts2的配置过程

(1)在项目中加入jar包

(2)web.xml中filter(过滤器)的配置

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/iKAFnweb-app_2_5.xsd">

struts2

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/iKAFnweb-app_2_5.xsd">

struts2

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

(3)struts.xml配置文件的编写

xmlnsiKAFn="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

xmlnsiKAFn="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

(4)aiKAFnction类的编写

package com.xmgc.sc.action;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.struts2.ServletActionContext;

public class MyUpLoadAction {

private String title;

private File upload;//与form表单中的file文件类型的名字是一样的

private String uploadContentType;//前缀必须要是上面的upload

private String uploadFileName;

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public File getUpload() {

return upload;

}

public void setUpload(File upload) {

this.upload = upload;

}

public String getUploadContentType() {

return uploadContentType;

}

public void setUploadContentType(String uploadContentType) {

this.uploadContentType = uploadContentType;

}

public String getUploadFileName() {

return uploadFileName;

}

public void setUploadFileName(String uploadFileName) {

this.uploadFileName = uploadFileName;

}

/* public String getSavePath() {

//ServletContext cxt=ServletActionContext.getServletContext();

//String path=cxt.getRealPath("/");

//这个获取的path为:http://localhost:8080/sc

//上传以后变为E:\software\apache-tomcat-6.0.45\webapps\sc

return savePath;

}

public void setSavePath(String savePath) {

//E:\software\apache-tomcat-6.0.45\webapps\sc/myupload

this.savePath = ServletActionContext.getServletContext().getRealPath("/myupload");

}*/

public String execute() throws IOException{

System.out.println(title);//标题

System.out.println(uploadContentType);//准备上传的文件的文件类型

System.out.println(uploadFileName);//准备上传的文件的文件名,记住还有扩展名

System.out.println(upload);

String realPath=ServletActionContext.getServletContext().getRealPath("/");

Strhttp://ing path=realPath+"myupload/"+uploadFileName;

System.out.println(realPath);

System.out.println(path);

FileInputStream fis=new FileInputStream(upload);

FileOutputStream fos=new FileOutputStream(path);

byte[] bytes=new byte[1024];//定义一个1024大小的字节数组

int len=-1;//用来做标志位

while((len=fis.read(bytes))>0){

fos.write(bytes, 0, len);

}

return null;

}

}

(5)jsP页面的编写

<%@ page contentType="text/html;charset=utf-8"%>

文件名:

上传:

经过这次总结,感觉struts2框架下的单文件的上传还是非常简单的,只要获取要存储在什么地方的地址,再通过输入输出流,写到这个地址中去就行了。绝大部分工作,struts2已经帮我们做好了。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信开发 微信授权详解
下一篇:微信和qq时间格式模板实例详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~