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来提 ...
随机推荐
- Codeforces Round #569 (Div. 2)A. Alex and a Rhombus
A. Alex and a Rhombus 题目链接:http://codeforces.com/contest/1180/problem/A 题目: While playing with geome ...
- 前端笔记之React(三)使用动态样式表&antd&React脚手架&props实战
一.使用动态样式表 1.1 LESS使用 全局安装Less npm install -g less 创建1.less文件,然后可以用lessc命令来编译这个文件: lessc 1.less 1.css ...
- ubuntu镜像快速下载
由于官网服务器在国外,下载速度奇慢,所以我们可以利用阿里云镜像下载ubuntu ubuntu 14.04: http://mirrors.aliyun.com/ubuntu-releases/14.0 ...
- Fabric1.4源码解析:客户端安装链码
看了看客户端安装链码的部分,感觉还是比较简单的,所以在这里记录一下. 还是先给出安装链码所使用的命令好了,这里就使用官方的安装链码的一个例子: #-n 指定mycc是由用户定义 ...
- python工具函数
1. translate translate要比replace要高效,translate支持替换多 使用translate之前必须要创建一个转换表.要创建转换表,可对字符串类型str调用方法maket ...
- spark入门(四)日志配置
1 背景 在测试spark计算时,将作业提交到yarn(模式–master yarn-cluster)上,想查看print到控制台这是很难的,因为作业是提交到yarn的集群上,所以,去yarn集群上看 ...
- Google 的 Java 编码规范,参考学习!
这份文档是 Google Java 编程风格规范的完整定义.当且仅当一个 Java 源文件符合此文档中的规则, 我们才认为它符合 Google 的 Java 编程风格. 与其它的编程风格指南一样,这里 ...
- windows container 踩坑记
windows container 踩坑记 Intro 我们有一些服务是 dotnet framework 的,不能直接跑在 docker linux container 下面,最近一直在折腾把它部署 ...
- VB非常见知识总结
1.VB.Net设置Excel中单元格字体 sheet.Range(sheet.Cells(row, stp), sheet.Cells(row, stp)).Font.Name = "Wi ...
- IDEA为新手专业打造
IDEA为新手专业打造 一.创建一个项目 新手的话可以先创建一个空项目 项目创建完成后会弹出一个Project Settings设置框,点击Project进行如图设置,设置完成点击OK 一.在创建的项 ...