在利用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. python3中django-admin找不到core的解决方法

    今天在测试django-admin startproject mysite时,出现以下问题: ImportError: No module named django.core 在执行此命令之前,我将d ...

  2. 1-2-K Game

    题目链接:https://vjudge.net/contest/330119#problem/E 题目大意可以理解为: 1.给出n个物品以及k,Alice与Bob轮流拿1个,或2个,或k个物品,Ali ...

  3. virtualbox 扩容存储

    本文讲解一种方式: 第一步: 我们需要将virtual box 设置成全局的环境变量 第二步: 找到要修改的xxx.vdi文件,通过命令来修改 下面操作步骤 1.设置virtual box 的环境变量 ...

  4. 洛谷P2178 [NOI2015]品酒大会 后缀数组+单调栈

    P2178 [NOI2015]品酒大会 题目链接 https://www.luogu.org/problemnew/show/P2178 题目描述 一年一度的"幻影阁夏日品酒大会" ...

  5. Docker pull 出现的 error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/

    vim /etc/sysconfig/docker OPTIONS='--selinux-enabled --log-driver=journald --signature-verification= ...

  6. shell使用ps -ef|grep xxx时不显示grep xxx进程的方法

    在使用ps -ef|grep xxx时会将grep xxx的进程也带出来, 而在脚本中如果想要截取此命令结果的一部分,则grep xxx的进程会显得多余,如下: [root@localhost ~]# ...

  7. 怎样设置HTTP请求头Header

    使用: xhr.setRequestHeader(); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.setRequest ...

  8. [Vue]method与计算属性computed、侦听器watch与计算属性computed的区别

    一.方法method与计算属性computed的区别 方法method:每当触发重新渲染时,调用方法method将总会再次执行函数: 计算属性computed:计算属性computed是基于它们的响应 ...

  9. [转载]IMDB文件格式

    [转载]IMDB文件格式 来源:LMDB的全称是Lightning Memory-Mapped Database,闪电般的内存映射数据库.它文件结构简单,一个文件夹,里面一个数据文件,一个锁文件.数据 ...

  10. SpringCloud 随笔

    目录 服务间通讯 统一配置中心 RabbitMQ Spring Cloud Stream 服务网关 Spring Cloud Zuul ++==(纯手打,代码可能有错!)==++ 服务间通讯 Rest ...