struts2学习笔记之十:文件上传
package com.djoker.struts2; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; public class uploadAction { private String myFileFileName; private File myFile; private String descContextType; public String getMyFileFileName() {
return myFileFileName;
} public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
} public File getMyFile() {
return myFile;
} public void setMyFile(File myFile) {
this.myFile = myFile;
} public String getDescContextType() {
return descContextType;
} public void setDescContextType(String descContextType) {
this.descContextType = descContextType;
} public String execute() throws Exception {
System.out.println(myFileFileName);
InputStream is = null;
OutputStream os = null;
try{
is = new BufferedInputStream(new FileInputStream(myFile));
os = new BufferedOutputStream(new FileOutputStream(ServletActionContext.getServletContext().getRealPath("upload") + "/" + myFileFileName));
byte[] ByteBuffer = new byte[1024];
int len = 0;
while((len = is.read(ByteBuffer)) > 0){
os.write(ByteBuffer, 0, len);
}
} finally {
if(is != null){
is.close();
}
if(os != null){
os.close();
}
}
return Action.SUCCESS;
}
}
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="uploadAction.action" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="myFile"><br>
文件描述:<input type="text" name="desc"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!-- 提供更加友好的提示信息 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 对字符集的设置 -->
<constant name="struts.i18n.encoding" value="GB18030"/>
<!-- 配置文件上传最大限制 -->
<constant name="struts.multipart.maxSize" value="9999999999"></constant>
<package name="struts2" extends="struts-default" >
<global-results>
<result>/success.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
<action name="login" class="com.djoker.struts2.LoginAction">
<result>/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="uploadAction" class="com.djoker.struts2.uploadAction">
<result>/success.jsp</result>
</action>
</package> <include file="struts-user.xml"></include>
</struts>
struts2学习笔记之十:文件上传的更多相关文章
- [原创]java WEB学习笔记49:文件上传基础,基于表单的文件上传,使用fileuoload 组件
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- PHP学习笔记--文件目录操作(文件上传实例)
文件操作是每个语言必须有的,不仅仅局限于PHP,这里我们就仅用PHP进行讲解 php的文件高级操作和文件上传实例我放在文章的最后部分.--以后我还会给大家写一个PHP类似于网盘操作的例子 注意:阅读此 ...
- Struts2学习(六)———— 文件上传和下载
一.单文件上传 在没学struts2之前,我们要写文件上传,非常麻烦,需要手动一步步去获取表单中的各种属性,然后在进行相应的处理,而在struts2中就不需要了,因为有一个fileUpload拦截器帮 ...
- (转载)JavaWeb学习总结(五十)——文件上传和下载
源地址:http://www.cnblogs.com/xdp-gacl/p/4200090.html 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传 ...
- JavaWeb学习总结(五十)——文件上传和下载
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
- PHP学习笔记 02 之文件上传
我们了解了表单传值后,这些我就可以完成PHP的文件上传了.我们了解PHP文件上传前,先了解PHP文件上传的原理. 一.PHP上传文件原理 第一步:将本地的文件通过form表单上传到服务器的临时目录中, ...
- Kali学习笔记38:文件上传漏洞
早些年,提到Web渗透,或者搜索一些黑客教程 基本都会看到文件上传漏洞. 它是一个很经典的漏洞 但它本质其实不是一个漏洞,而是网站本身的上传文件功能 不过如果我们上传了Webshell,那么就成为了文 ...
- SpringMVC学习笔记八:文件上传下载(转)
转自:http://www.cnblogs.com/WJ-163/p/6269409.html 一.关键步骤 ①引入核心JAR文件 SpringMVC实现文件上传,需要再添加两个jar包.一个是文件上 ...
- SpringBoot学习笔记(8)-----SpringBoot文件上传
直接上代码,上传文件的前端页面: <body> <form action="/index/upload" enctype="multipart/form ...
随机推荐
- 两种状态显示处理. enum , Linq AsEnumerable
1.ENUM protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { GridViewRow ro ...
- Spring 笔记
依赖注入(Dependency Injection DI) 通过依赖注入,对象的依赖关系将由系统中负责协调对象的第三方组件在创建对象的时候进行设定.(p6 spring之旅) 在创建类的时候需要依赖的 ...
- 2008ISBN号码
题目描述 Description 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符( ...
- Ubuntu 16.04 启动错误 "a start job is running for hold until boot process finishes up"
老司机也差点翻船... 升级16.04的时候,将默认启动管理器(default display manager)选为gm3(gnome3)了(应该使用默认的lightgm)如果改成gm3,好像是nvi ...
- CodeForces 743C Vladik and fractions (数论)
题意:给定n,求三个不同的数满足,2/n = 1/x + 1/y + 1/z. 析:首先1是没有解的,然后其他解都可以这样来表示 1/n, 1/(n+1), 1/(n*(n+1)),这三个解. 代码如 ...
- 3_for循环的拓展应用
HTML代码: <input type="button" value="1"/> <input type="button" ...
- 经验总结之Android framework开发
本人在某做手机ROM的厂商工作近两年,虽然工作年限不长,但是对此类“工种”已有比较初步的认识,写下来供大家参考借鉴. framework的工作相对于其他比较成熟的软件开发类似,分为维护和需求开发. 需 ...
- oracle基本操作
登入oraclesqlplus / as sysdba启动oraclestartup停止oracleshutdown 创建新用户create user username identified by p ...
- uva-10305
题意:给出n个任务,任务不是完全独立的,有些任务必须依赖另外一些任务才能执行:m个任务关系. 输出:n个任务的可能执行顺序: 我的解决方法:这就是个赤裸裸的拓扑排序,直接dfs拓扑每一个任务点,然 ...
- 第五章GPIO接口
5.1 GPIO硬件介绍 可以不通过他们输出高低电平或者通过它们读入应交的状态 S3C2410有117个I/O端口,分为A~H共8组:GPA.GPB....GPH S3C2440有130个I/O端口, ...