Struts2实现上传单个文件功能

网友投稿 234 2023-05-10


Struts2实现上传单个文件功能

upload.jsp 这个页面选择提交文件,提交到uploadImage.action

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

pageEncoding="utf-8"%>

please select the file:

FileUploadAction.java 将传来的file进行处理

package action;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import org.apache.commons.io.IOUtils;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{

private File upload;

private String uploadFileName;

private String uploadContentType;

public File getUpload() {

return upload;

}

public void setUpload(File upload) {

this.upload = upload;

}

public String getUploadFileName() {

return uploadFileName;

}

public void setUploadFileName(String uploadFileName) {

this.uploadFileName = uploadFileName;

}

public String getUploadContentType() {

return uploadContentType;

}

public void setUploadContentType(String uploadContentType) {

this.uploadContentType = uploadContentType;

}

public String execute(){

System.out.println(upload);

System.out.println(uploadContentType);

System.out.println(uploadFileName);

String savePath = ServletActionContext.getServletContext().getRealPath("/upload/"+this.uploadFileName);

System.out.println(savePath);

try{

FileInputStream fis = new FileInputStream(upload);

FileOutputStream fos = new FileOutputStream(savePath);

IOUtils.copy(fis, fos);

fos.flush();

fos.close();

fis.close();

}catch(Exception e){

e.printStackTrace();

}

return "success";

}

}

uploadFileName和uploadContentType,这两个属性分别用于封装上传文件的文件名、上传文件的文件类型

Struts.xml配置

uploadSuccess.jsp

uploadError.jsp

成功失败界面随便写一个就行了,不贴了。


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

上一篇:angular中使用Socket.io实例代码
下一篇:Java线程安全的常用类_动力节点Java学院整理
相关文章

 发表评论

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