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来提 ...
随机推荐
- 【React】react学习笔记01-概念与基本使用
俺为啥要学这玩意: 家里的杂事好不容易处理完了,开始正式静下心来学习~博主是做后端开发的,js基础不深,之前也是用React写过许多东西了,但是基本上都是用的CV大法,别人的组 件修修改改拿来 ...
- php回调函数设计
<?php namespace Server; /** * 回调函数设计 * Class Server * @package Server */ class Server { public fu ...
- Rstudio调用plot()函数时,出现错误的处理方法
按照书上的例子敲出代码后,发现Rstudio无法识别C盘user文件夹下的中文用户名.如下图所示: 按照网上的做法,尝试修改计算机user下的用户名,没修改成功. 另一种做法是在plot()函数前面加 ...
- redhat6.0下配置DNS
最近操作系统要结课,老师要求在redhat上配置各种服务器角色,包括dhcp.ftp.web.dns.前三个都还好,但就dns,被折磨的死去活来的,真让人头大.还好在同学的帮助下最后配置成功,实现了正 ...
- mysql报错(Not unique table/alias)
Not unique table/alias 错误编号:1066 问题分析: SQL 语句中出现了非唯一的表或别名. 解决方法: 1.请检查出现问题位置的 SQL 语句中是否使用了相同的表名,或是定义 ...
- HDU 1724:Ellipse(自适应辛普森积分)
题目链接 题意 给出一个椭圆,问一个[l, r] 区间(蓝色区域)的面积是多少. 思路 自适应辛普森积分 具体一些分析如上. 很方便,套上公式就可以用了. 注意 eps 的取值影响了跑的时间,因为决定 ...
- scrapy基础知识之 关于爬虫部分一些建议:
1.尽量减少请求次数,能抓列表页就不抓详情页,减轻服务器压力,程序员都是混口饭吃不容易. 2.不要只看 Web 网站,还有手机 App 和 H5,这样的反爬虫措施一般比较少. 3.实际应用时候,一般防 ...
- 1. 在Mac OS中配置CMake的详细图文教程
CMake是一个比make更高级的跨平台的安装.编译.配置工具,可以用简单的语句来描述所有平台的安装(编译过程).并根据不同平台.不同的编译器,生成相应的Makefile或者project文件.本文主 ...
- 宏旺半导体深度剖析嵌入式存储芯片eMMC原理 一篇概括大全
eMMC 一直是嵌入式存储市场最主流的选择,除了读写速度快.性价比高外,在节省空间方面也是相当优秀,今天宏旺半导体就和大家详细聊聊eMMC. eMMC 是 embedded MultiMediaCar ...
- (转)代码结构中Dao,Service,Controller,Util,Model是什么意思?
作者:技能树IT修真院链接:https://www.zhihu.com/question/58410621/answer/623496434来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商 ...