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来提 ...
随机推荐
- 【设计模式】行为型07备忘录模式(Memento Pattern)
参考地址:http://www.runoob.com/design-pattern/memento-pattern.html 对原文总结调整,以及修改代码以更清晰的展示: 备忘录模式(快照模式): ...
- vue的懒加载如何实现?
个人通过查找发现一个比较好用的模块,vue-lazyload 第一步 下载安装这个包 npm install vue-lazyload 第二步 在main.js中引入这个模块 import Vu ...
- CentOS下安装配置MySQL8.0的步骤详解
下载yum源的安装包 yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm 安装 yu ...
- Java连载2-Java特性
一.JDK 1.含义:Java开发工具包. 2.做Java开发之前必须安装的一个工具包,下载地址:https://www.oracle.com/index.html 3.Java包括三大块内容: ( ...
- Visual Studio中View页面与Js页面用快捷键互相跳转
现在已经将源码放到GitHub中了 地址是 https://github.com/liningit/ViewJsLN 公司开发的项目使用的是Mvc框架,且Js与View页面是分开在两个文件夹下的,所以 ...
- Java虚拟机详解(三)------垃圾回收
如果对C++这门语言熟悉的人,再来看Java,就会发现这两者对垃圾(内存)回收的策略有很大的不同. C++:垃圾回收很重要,我们必须要自己来回收!!! Java:垃圾回收很重要,我们必须交给系统来帮我 ...
- python接口自动化(三十三)-python自动发邮件总结及实例说明番外篇——下(详解)
简介 发邮件前我们需要了解的是邮件是怎么一个形式去发送到对方手上的,通俗点来说就是你写好一封信,然后装进信封,写上地址,贴上邮票,然后就近找个邮局,把信仍进去,其他的就不关心了,只是关心时间,而电子邮 ...
- Java学习笔记之---集合
Java学习笔记之---集合 (一)集合框架的体系结构 (二)List(列表) (1)特性 1.List中的元素是有序并且可以重复的,成为序列 2.List可以精确的控制每个元素的插入位置,并且可以删 ...
- [转载] 管Q某犇借的手写堆
跟gxy大神还有yzh大神学了学手写的堆,应该比stl的优先队列快很多. 其实就是维护了一个二叉堆,写进结构体里,就没啥了... 据说达哥去年NOIP靠这个暴力多骗了分 合并果子... templat ...
- cogs426血帆海盗(网络流打法)
这道题基本上来就能看的出来是网络流但难点在于它的结果貌似与最大流,最小割都没啥关系,我猜不少萌新像我一样想暴力枚举边(要是考试我就真这么做了),但很明显,出题人没打算让你这么水过,100000的数据不 ...