分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

Struts2之上传下载

发布时间:2023-09-06 02:09责任编辑:赖小花关键词:暂无标签

单文件上传

上传页面

<%@ page language="java" contentType="text/html; charset=UTF-8" ???pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action="upload.action" method="post" enctype="multipart/form-data"> ???文件:<input type="file" name="upload"><br><br> ???上传者:<input type="text" name="author"> ???<input type="submit" value="上传"></form></body></html>

struts.xml配置

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC ???"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" ???"http://struts.apache.org/dtds/struts-2.5.dtd"><struts><constant name="struts.devMode" value="true"></constant><package name="jiangwenwen" namespace="/" extends="struts-default"> ???<action name="upload" class="cn.jiangwenwen.action.UploadAction" method="fileUpload"> ???????<result name="success">/success.jsp</result> ???</action></package></struts>

Action类

public class UploadAction ?extends ActionSupport{ ???????//存放上传的文件对象 ???private File upload; ???????//上传的文件名称 ???private String uploadFileName; ???//上传的上传者 ???private String author; ???????public String fileUpload() throws IOException { ???????????????FileInputStream fis = new FileInputStream(upload); ???????????????String path = "D://pic/"+uploadFileName; ???????????????FileOutputStream fos = new FileOutputStream(path); ???????????????int flag = 0; ???????????????while((flag=fis.read())!=-1) { ???????????fos.write(flag); ???????} ???????fis.close(); ???????fos.close(); ???????????????return SUCCESS; ???????????} ???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 getAuthor() { ???????return author; ???} ???public void setAuthor(String author) { ???????this.author = author; ???} ???}

成功接收页面

<%@ page language="java" contentType="text/html; charset=UTF-8" ???pageEncoding="UTF-8" isELIgnored="false"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><!-- 此 /pic为服务器配置的路径--><img src="/pic/${uploadFileName }"></body></html>

Struts2之上传下载

原文地址:https://www.cnblogs.com/jiangwenwen1/p/9465385.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved