struts1.2上传文件到服务器
利用了struts1.2的jar包,如何添加jar包:如果用Eclipse可以在项目上右键直接从MyEclipse-->Add struts .
如图
然后开始编码,步骤如下:
1.页面:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="gb2312"%>
<html>
<body>
<form action="<%=request.getContextPath() %>/admin/HtmlFile.do" method="post" enctype="multipart/form-data" name="HtmlFileForm">
<input type="file" name="file" />
<html:submit />
</form>
</body>
</html>
2.写文件Form
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class HtmlFileForm extends ActionForm {
private static final long serialVersionUID = -8008580023746850586L;
public HtmlFileForm() {
}
private FormFile file;
public FormFile getFile() {
return this.file;
}
public void setFile(FormFile file) {
this.file = file;
}
private String fname;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
private String size;
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
}
3.写Action
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class HtmlFileAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String dir = servlet.getServletContext().getRealPath("/upload");
HtmlFileForm hff = (HtmlFileForm) form;
FormFile file = hff.getFile();
// if no file was uploaded,then display View
if (file == null) {
return mapping.findForward("fail");
}
// Get name and file size
String fname = file.getFileName();
String size = Integer.toString(file.getFileSize()) + "bytes";
InputStream streamIn = file.getInputStream();
OutputStream streamOut = new FileOutputStream(dir + "/" + fname);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while((bytesRead = streamIn.read(buffer,0,8192))!=-1){
streamOut.write(buffer,0,bytesRead);
}
streamOut.close();
streamIn.close();
//
hff.setFname(fname);
//Clean up our toys when done playing
file.destroy();
//Forward to default display
request.setAttribute("inputfile", fname);
return mapping.findForward("success");
}
}
__________________________________________________________________
可以到这里下载源码:http://dl.javaeye.com/topics/download/db0c7116-3a8e-3a6d-9dde-2cc82e0c5f50
需要先注册账号。
http://yuanyuan7891.javaeye.com/blog/711312
struts1.2上传文件到服务器相关推荐
- C 上传文件到服务器(含接收端源码)
2019独角兽企业重金招聘Python工程师标准>>> 本文demo下载地址:http://www.wisdomdd.cn/Wisdom/resource/articleDetail ...
- android 上传文件到服务器
1.编写layout.xml <LinearLayout android:layout_width="match_parent"android:layout_height=& ...
- c winform 上传文件到mysql_WinForm上传文件至服务器
/// /// WebClient上传文件至服务器 /// /// 文件名,全路径格式 /// 服务器文件夹路径 /// public bool Upload(string localFilePath ...
- 上传文件到服务器并显示,J2EE如何实现Servlet上传文件到服务器并相应显示功能...
J2EE如何实现Servlet上传文件到服务器并相应显示功能 发布时间:2021-07-10 11:44:54 来源:亿速云 阅读:82 作者:小新 这篇文章将为大家详细讲解有关J2EE如何实现Ser ...
- linux上传文件到服务器
1.连接服务器 : ssh 用户名@ip地址 例如: unbutu@unbutu-System-Product-Name:~$ ssh root@123.57.235.11 root@123.57.2 ...
- jsch 移动服务器上文件,jsch上传文件到服务器
需求就是上传文件到服务器,服务器的存储地址由程序决定然后可以自动创建. 使用第三方:jsch JSch 是SSH2的一个纯Java实现.它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文 ...
- android -上传文件到服务器
android上传文件到服务器 重点:最好是设置好content-type这些参数的配置! package com.spring.sky.image.upload.network; ...
- Android上传文件至服务器(转)
本实例实现每隔5秒上传一次,通过服务器端获取手机上传过来的文件信息并做相应处理:采用Android+Struts2技术. 一.Android端实现文件上传 1).新建一个Android项目命名为and ...
- scp 上传文件到服务器
scp 上传文件到服务器 scp命令 scp 本地路径 服务器路径 如 scp /Users/***/Desktop/text.zip root@****:/opt/ 本来是想通过"scp ...
最新文章
- 53 Paramiko的使用
- 使用tensorflow预测函数的参数值(a simple task)
- 使用pil读取gif图有些位置为黑色_使用 Pillow 快速创建 GIF 动图
- HBase删除和修改操作
- 高等数理统计(part1)--随机变量及其分布函数
- 据悉,深圳某工程师沦为C语言笔试枪手
- 面向对象(特点)、局部变量与全局变量的区别、匿名对象、构造函数、
- sql script: Calculating Days
- mysql error report,ECSHOP网店系统提示MYSQL SERVER ERROR REPORT的解决方法
- UI设计灵感|音乐播放器界面如何设计?
- 东京疫情数据开源当日斩获2k+ star,GitHub疫情项目源源不断
- Python之路 - 网络编程之Socket
- 网络编程项目——在线电子词典
- 从 Java 到 Kotlin,再从 Kotlin 回归 Java
- python面板数据模型操作步骤_面板数据分析方法步骤全解
- 绘制盐沼植被图的混合方法-文献阅读
- 概率论在实际生活的例子_日常生活中的概率统计
- pyton入门 — 其他常用语法
- 用vue实现类ant Design的日历组件
- html像素测量尺,屏幕尺子:ruul.Screen ruler