利用Struts上传文件
在利用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上传文件的更多相关文章
- SAE利用storge上传文件 - myskies的专栏 - 博客频道 - CSDN.NET
SAE利用storge上传文件 - myskies的专栏 - 博客频道 - CSDN.NET SAE利用storge上传文件
- php 利用socket上传文件
php 利用socket上传文件 张映 发表于 2010-06-02 分类目录: php 一,利用fsockopen来上传文件 以前我写过一篇关于socket通信原理的博文http://blog.51 ...
- Struts上传文件
Struts上传文件分为两个步骤: 1). 首先将客户端上传的文件保存到Struts.multipart.saveDir键所指定的目录中,如果该键所对应的目录不存在,那么就保存到javax.servl ...
- 利用struts2上传文件时,如果文件名中含有-符号,那么会出错
利用struts2上传文件时,如果文件名中含有-符号,那么会出错 报错如下: HTTP Status 500 - C:\Program Files\Apache Software Foundation ...
- Struts 上传文件
1. 客户端注意事项 method="post" enctype="multipart/form-data" <input type="file ...
- struts 上传文件 Dynavalidatorform 实例
一.相关jar包 一个空struts工程的jar包: 另上传文件的两个jar包: 二.页面 1.上传页面upload.jsp <%@ page language="jav ...
- shell中利用ftp 上传文件夹功能
我们知道ftp 只能用来上传或者下载文件,一次单个或者多个,怎么实现将文件夹的上传和下载呢? 可以利用先在remote ip上建立一个相同的文件夹目录,然后将文件放到各自的目录中去 1.循环遍历出要上 ...
- 利用git上传文件到github
git add 文件名称/. "."代表全部 git commit -m -a git push -u origin master 推送到远程仓库 ---------------- ...
- struts上传文件 血案
记录一个图片上传之后没有后缀 拓展名问题 平常我们查询数据都是 fileImage=fileImageService.getQuery(); 让entity等于它 那么fileImage.getF ...
随机推荐
- 【计算机视觉】opencv读取多个摄像头
[计算机视觉]opencv读取多个摄像头 标签(空格分隔): [图像处理] 说明:今天蹭了机器视觉课程,讲到了stereopsis,立体视觉,讲到了关于通过多个摄像头获取object的depth信息的 ...
- HttpHelper帮助类
public class HttpHelper { #region 预定义方变量 //默认的编码 private Encoding _encoding = Encoding.Default; //Po ...
- $.ajax 中的contentType类型
参考链接:https://www.jianshu.com/p/f4d92b3d387d
- UWP笔记-边框背景虚化效果
这是一个简单的UI外观 1.添加Negut包: Microsoft.Toolkit.Uwp.UI.Controls 2.xaml:命名空间中引用 xmlns:controls="using: ...
- ubuntu中配置ip地址和上网配置
ubuntu中的ip地址配置: 基于NAT模式: 如果能正常连接ip,但不能ping动www.baidu.com的话,我们需要在配置文件中配置主机上网的DNS地址,如下: 如果上面图片上的DNS se ...
- Linux系列之压缩与解压
1.压缩技术 1.常用命令实例 1.zip格式的压缩与解压缩 zip是压缩指令,unzip是解压指令.zip指令既可以压缩文件,也可以压缩目录.压缩会自动保留源文件,解压会自动保留压缩文件. zip ...
- gcc命令-更新中....
下载安装MinGW 1.编译c 使用gcc xx.c命令,将文件编译为a.exe.或使用gcc xx.c -o xx.exe命令,将文件编译为xx.exe 2.编译c++ 使用g++ xx.cpp命令 ...
- C++ STL String学习 (待续)
头文件:<string> 字符串类初始化: string s1="aabbba"; s2=string("bbb"); string s3=stri ...
- C++ 继承 - 在派生类中对基类初始化
构造函数与基类的其他成员不同,不能被派生类继承,因此为了初始化基类中的成员变量,需要在派生类中调用基类的构造函数(即显式调用),如果派送类没有调用则默认调用基类的无参构造函数(即隐式调用). 显式调用 ...
- LeetCode 答案(python)1-17
1.给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], targe ...