在利用struts2完成上传文件到服务器时,遇到获取不到文件名

原因是在Action中的属性名没有和jsp中的属性名匹配

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'main.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>上传文件</h1>
<form action="upload" method="post" enctype="multipart/form-data">
请选择文件:<input type="file" name="file"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

Action:

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.ActionSupport; public class Upload extends ActionSupport{ private File file;
private String fileContentType;
private String fileFileName;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
} public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String upload(){
System.out.println(this.fileContentType);
System.out.println(this.fileFileName); InputStream is=null;
try {
is=new FileInputStream(file);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} String path=ServletActionContext.getServletContext().getRealPath("/");
System.out.println(path); File toFile=new File(path, this.getFileFileName()); OutputStream os=null;
try {
os=new FileOutputStream(toFile);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} byte[] buffer=new byte[1024];
int length=0;
try {
while((length=is.read())>0){
os.write(buffer, 0, length);
}
is.close();
os.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return SUCCESS;
} }

Action中该这样配置

private File file;
 private String fileContentType;
 private
String fileFileName;

ContentType和FileName是固定的,前缀为<input>中的name的值

利用Struts上传文件的更多相关文章

  1. SAE利用storge上传文件 - myskies的专栏 - 博客频道 - CSDN.NET

    SAE利用storge上传文件 - myskies的专栏 - 博客频道 - CSDN.NET SAE利用storge上传文件

  2. php 利用socket上传文件

    php 利用socket上传文件 张映 发表于 2010-06-02 分类目录: php 一,利用fsockopen来上传文件 以前我写过一篇关于socket通信原理的博文http://blog.51 ...

  3. Struts上传文件

    Struts上传文件分为两个步骤: 1). 首先将客户端上传的文件保存到Struts.multipart.saveDir键所指定的目录中,如果该键所对应的目录不存在,那么就保存到javax.servl ...

  4. 利用struts2上传文件时,如果文件名中含有-符号,那么会出错

    利用struts2上传文件时,如果文件名中含有-符号,那么会出错 报错如下: HTTP Status 500 - C:\Program Files\Apache Software Foundation ...

  5. Struts 上传文件

    1. 客户端注意事项 method="post" enctype="multipart/form-data" <input type="file ...

  6. struts 上传文件 Dynavalidatorform 实例

    一.相关jar包     一个空struts工程的jar包:    另上传文件的两个jar包: 二.页面 1.上传页面upload.jsp <%@ page language="jav ...

  7. shell中利用ftp 上传文件夹功能

    我们知道ftp 只能用来上传或者下载文件,一次单个或者多个,怎么实现将文件夹的上传和下载呢? 可以利用先在remote ip上建立一个相同的文件夹目录,然后将文件放到各自的目录中去 1.循环遍历出要上 ...

  8. 利用git上传文件到github

    git add 文件名称/. "."代表全部 git commit -m -a git push -u origin master 推送到远程仓库 ---------------- ...

  9. struts上传文件 血案

    记录一个图片上传之后没有后缀 拓展名问题 平常我们查询数据都是  fileImage=fileImageService.getQuery();  让entity等于它 那么fileImage.getF ...

随机推荐

  1. 华为Fusioncompute 6.5.1

    V6.5.1VRM“gandalf”用户的默认密码为“IaaS@OS-CLOUD9!”.CNA“gandalf”用户的默认密码为“IaaS@OS-CLOUD8!”.普通模式:admin/LaaS@PO ...

  2. Python学习笔记——类和对象

    类和对象 1. 一个例子 # 对象 = 属性 + 方法 # Python 中的类名称约定首字母大写,而 Python 函数名首字母小写 class Turtle: #属性 color = 'green ...

  3. shell学习笔记1-文件安全与权限

    1,创建文件的用户和他所属的组拥有该文件,文件的属主可以设定谁具有读.写.执行该文件的权限,根用户可以改变任何普通用户的设置. 2,一个文件一经创建,就具有三种访问权限:读(可以显示该文件的内容).写 ...

  4. 最新 猎豹移动java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.猎豹移动等10家互联网公司的校招Offer,因为某些自身原因最终选择了猎豹移动.6.7月主要是做系统复习.项目复盘.Leet ...

  5. 阻止移动端input按钮聚焦时唤起软键盘的方法

    一.设置input为readonly 二.使用JS代码,在input按钮fous时就让其blur

  6. rdbtool

    https://www.cnblogs.com/wjoyxt/p/10577361.html https://github.com/sripathikrishnan/redis-rdb-tools h ...

  7. Design Compressed String Iterator

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

  8. ArrayList集合详解

    ArrayList 实现了List的接口,是长度可变的数组,空间是连续的 api默认提供了很多操作ArrayLis的方法,这些方法可以去api里面查询使用 一.这么多方法怎么学?1.熟练使用常见的方法 ...

  9. Linux IO模式以及select poll epoll详解

    一 背景 同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 本文讨论的背景是Linux环境下的network ...

  10. 1.4Zookeeper和Thymeleaf的使用

    什么是Zookeeper? Zookeeper 是一个开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务.配置维护和命名服务等等.采用下图描述zookeep ...