(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 上传文件的更多相关文章

  1. IE8/9 JQuery.Ajax 上传文件无效

    IE8/9 JQuery.Ajax 上传文件有两个限制: 使用 JQuery.Ajax 无法上传文件(因为无法使用 FormData,FormData 是 HTML5 的一个特性,IE8/9 不支持) ...

  2. 三种上传文件不刷新页面的方法讨论:iframe/FormData/FileReader

    发请求有两种方式,一种是用ajax,另一种是用form提交,默认的form提交如果不做处理的话,会使页面重定向.以一个简单的demo做说明: html如下所示,请求的路径action为"up ...

  3. asp.net mvc 上传文件

    转至:http://www.cnblogs.com/fonour/p/ajaxFileUpload.html 0.下载 http://files.cnblogs.com/files/fonour/aj ...

  4. app端上传文件至服务器后台,web端上传文件存储到服务器

    1.android前端发送服务器请求 在spring-mvc.xml 将过滤屏蔽(如果不屏蔽 ,文件流为空) <!-- <bean id="multipartResolver&q ...

  5. .net FTP上传文件

    FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...

  6. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  7. 前端之web上传文件的方式

    前端之web上传文件的方式 本节内容 web上传文件方式介绍 form上传文件 原生js实现ajax上传文件 jquery实现ajax上传文件 form+iframe构造请求上传文件 1. web上传 ...

  8. Django session cookie 上传文件、详解

    session 在这里先说session 配置URL from django.conf.urls import patterns, include, url from django.contrib i ...

  9. 4 django系列之HTML通过form标签来同时提交表单内容与上传文件

    preface 我们知道提交表单有2种方式,一种直接通过submit页面刷新方法来提交,另一种通过ajax异步局部刷新的方法提交,上回我们说了通过ajax来提交文件到后台,现在说说通过submit来提 ...

随机推荐

  1. QT 资料收集 (不定期添加)

    Qt之界面实现技巧 http://blog.sina.com.cn/s/blog_a6fb6cc90101dech.html

  2. 5分钟快速部署DataDraw数字绘

    经常有小伙伴问,有没有一款好用又免费的画图软件,画画流程图.UML.思维导图?今天就介绍一款开源的在线画图软件,满足各方面人的需求. DataDraw数字绘是一个在线线框图.流程图.网络图.组织结构图 ...

  3. Codeforces 305B:Continued Fractions(思维+gcd)

    http://codeforces.com/problemset/problem/305/B 题意:就是判断 p / q 等不等于那条式子算出来的值. 思路:一开始看到 1e18 的数据想了好久还是不 ...

  4. 【搜索引擎】 PostgreSQL 10 实时全文检索和分词、相似搜索、模糊匹配实现类似Google搜索自动提示

    需求分析 要通过PostgreSQL实现类似Google搜索自动提示的功能,例如要实现一个查询海量数据中的商品名字,每次输入就提示用户各种相关搜索选项,例如淘宝.京东等电商查询 思路 这个功能可以用 ...

  5. SQL参数化查询

    参数化查询(Parameterized Query 或 Parameterized Statement)是指在设计与数据库链接并访问数据时,在需要填入数值或数据的地方,使用参数 (Parameter) ...

  6. 基于lua-nginx-module(openresty)的WEB应用防火墙

    独乐乐,不如众乐乐,分享给大家一篇WEB应用防火墙的文章,基于Lua+ Nginx实现.以下是ngx_lua_waf的作者全文输出. Github地址:https://github.com/loves ...

  7. python3.5学习笔记(第六章)

    本章内容: 正则表达式详解(re模块) 1.不使用正则表达式来查找文本的内容 要求从一个字符串中查找电话号码,并判断是否匹配制定的模式,如:555-555-5555.传统的查找方法如下: def is ...

  8. vim 复制大块内容。 y,p(是单个y,而不是yy)

    vim 复制大块内容. y,p(是单个y,而不是yy)

  9. 洛谷P2299 Mzc和体委的争夺战 题解

    题目 题目描述 mzc家很有钱(开玩笑),他家有n个男家丁(做过前三弹的都知道).但如此之多的男家丁吸引来了我们的体委(矮胖小伙),他要来与mzc争夺男家丁. mzc很生气,决定与其决斗,但cat的体 ...

  10. Excel催化剂开源第8波-VSTO开发之异步调用方法

    在VSTO开发过程中,因其和普通的Winform开发有点差别,具体细节笔者也说不清楚,大概是VSTO的插件是寄生在Excel中,不属于独立的进程之类的,其异步方法调用时,未能如Winform那样直接用 ...