Struts2实现上传单个文件功能

时间:2021-05-20

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

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://.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配置

<action name="uploadImage" class="action.FileUploadAction"> <result name="success">uploadSuccess.jsp</result> <result name="input">uploadError.jsp</result></action>

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章