strus 上传文件
(1) action代码
package comSys.struts.articleManager; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport;
import comSys.service.ArticleService; public class UploadArticleAction extends ActionSupport {
private File upload; //上传的文件
private String uploadFileName; //文件名称
private String uploadContentType; //文件类型
private String enableVisit;
private String enableDownload;
private int id; //所属类型id
private String message;
private String des;
private ArticleService service; public String execute() throws Exception {
String realpath = ServletActionContext.getServletContext().getRealPath("/upload");
Object username=ServletActionContext.getRequest().getSession().getAttribute("username");
Object jobNum=ServletActionContext.getRequest().getSession().getAttribute("jobNum");
if(username==null||username.equals("")||jobNum==null||jobNum.equals("")){ //未登录要求重新登录
return ERROR;
}else if (upload != null) {
File file=new File(realpath,username.toString());
if(!file.exists()){
file.mkdirs();
}
File savefile = new File(file, uploadFileName);
FileOutputStream fo = new FileOutputStream(savefile);
FileInputStream fi = new FileInputStream(upload);
byte[] buffer = new byte[1024];
int len = 0;
while((len = fi.read(buffer))!=-1){
fo.write(buffer,0,len);
}
fo.close();
fi.close(); boolean enableDownloadBool=enableDownload.equals("0")?false:true;
boolean enableVisitBool=enableVisit.equals("0")?false:true;
service=new ArticleService();
if(service.addArticle(uploadFileName, id, enableDownloadBool,enableVisitBool , "/upload/"+username.toString(),des)>-1){
this.setMessage("文件上传成功!!");
}else
this.setMessage("文件上传失败!!");
}
return SUCCESS;
} public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getEnableVisit() {
return enableVisit;
}
public void setEnableVisit(String enableVisit) {
this.enableVisit = enableVisit;
}
public String getEnableDownload() {
return enableDownload;
}
public void setEnableDownload(String enableDownload) {
this.enableDownload = enableDownload;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public String getDes() {
return des;
} public void setDes(String des) {
this.des = des;
} }
(2) jsp
<form action="<%=path %>/UploadArticleAction.action" method="post" enctype="multipart/form-data">
<table>
<tr><td>上传文件:</td><td><input type="file" name="upload" id="upload"></td></tr>
<tr><td>是否允许访问:</td><td><input type="radio" name="enableVisit" value="0">不允许访问<input type="radio" name="enableVisit" value="1">允许访问</td></tr>
<tr><td>是否允许下载:</td><td><input type="radio" name="enableDownload" value="0">不允许下载<input type="radio" name="enableDownload" value="1">允许下载</td></tr>
<tr><td colspan="2">
<span style="text-align:center;">描述:</span><textarea name="des" id="des" cols="50" rows="5"></textarea>
</td></tr>
<tr><td colspan="2" align="right">
<input type="button" value="确定" style="margin-right: 10px;" onclick="addArticle()">
<input type="button" value="取消" style="margin-right: 10px;" onclick="cancle()">
<input type="hidden" value="${id}" name="id">
</td>
</tr>
</table>
</form>
strus 上传文件的更多相关文章
- IE8/9 JQuery.Ajax 上传文件无效
IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...
- 三种上传文件不刷新页面的方法讨论:iframe/FormData/FileReader
发请求有两种方式,一种是用ajax,另一种是用form提交,默认的form提交如果不做处理的话,会使页面重定向.以一个简单的demo做说明: html如下所示,请求的路径action为"up ...
- asp.net mvc 上传文件
转至:http://www.cnblogs.com/fonour/p/ajaxFileUpload.html 0.下载 http://files.cnblogs.com/files/fonour/aj ...
- app端上传文件至服务器后台,web端上传文件存储到服务器
1.android前端发送服务器请求 在spring-mvc.xml 将过滤屏蔽(如果不屏蔽 ,文件流为空) <!-- <bean id="multipartResolver&q ...
- .net FTP上传文件
FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...
- 通过cmd完成FTP上传文件操作
一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...
- 前端之web上传文件的方式
前端之web上传文件的方式 本节内容 web上传文件方式介绍 form上传文件 原生js实现ajax上传文件 jquery实现ajax上传文件 form+iframe构造请求上传文件 1. web上传 ...
- Django session cookie 上传文件、详解
session 在这里先说session 配置URL from django.conf.urls import patterns, include, url from django.contrib i ...
- 4 django系列之HTML通过form标签来同时提交表单内容与上传文件
preface 我们知道提交表单有2种方式,一种直接通过submit页面刷新方法来提交,另一种通过ajax异步局部刷新的方法提交,上回我们说了通过ajax来提交文件到后台,现在说说通过submit来提 ...
随机推荐
- Lombok简介及入门使用 (转载)
Lombok简介及入门使用 lombok既是一个IDE插件,也是一个项目要依赖的jar包. Intellij idea开发的话需要安装Lombok plugin,同时设置 Setting -> ...
- php+mysql新无线级分类
create table cat( id int unsigned not null primary key auto_increment, pid , path ) not null default ...
- 跟我学SpringCloud | 第八篇:Spring Cloud Bus 消息总线
SpringCloud系列教程 | 第八篇:Spring Cloud Bus 消息总线 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如无特 ...
- 使用CocoaPods创建自己的私有库-iOS组件化第一步
目前iOS组件化常用的解决方案是Pod+路由+持续集成,通常架构设计完成后第一步就是将原来工程里的模块按照架构图分解为一个个独立的pod工程(组件),今天我们就来看看如何创建一个Pod私有库. 新建: ...
- 设计模式-责任链模式(responsibility)
责任链模式是行为模式的一种,该模式构造一系列的分别担当不同职责的类的对象(HeaderCar.BodyCar.FooterCar)来共同完成一个任务,这些类的对象之间像链条一样紧密相连. 角色和职责: ...
- Linux查看进程具体开启时间
ps -p 2417 -o lstart -- 2417为进程号
- Flutter学习笔记(5)--Dart运算符
如需转载,请注明出处:Flutter学习笔记(5)--Dart运算符 先给出一个Dart运算符表,接下来在逐个解释和使用.如下: 描述 ...
- django基础知识之URLconf:
URLconf 在settings.py文件中通过ROOT_URLCONF指定根级url的配置 urlpatterns是一个url()实例的列表 一个url()对象包括: 正则表达式 视图函数 名称n ...
- C++学习书籍推荐《Exceptional C++》下载
百度云及其他网盘下载地址:点我 编辑推荐 <Exceptional C++:47个C++工程难题.编程问题和解决方案(中文版)>中的每个问题都给出了难度系数,在这些问题中阐释一些微妙的编程 ...
- 数据结构-双向链表(Python实现)
数据结构在编程世界中一直是非常重要的一环,不管是开发还是算法,哪怕是单纯为了面试,数据结构都是必修课,今天我们介绍链表中的一种--双向链表的代码实现. 好了,话不多说直接上代码. 双向链表 首先,我们 ...