使用Struts2标签实现文件上传
来源:互联网 发布:差评拦截软件 编辑:程序博客网 时间:2024/05/22 17:12
第一步:
Jsp页面中:
<%@taglib prefix="s" uri="/struts-tags" %>
<tr>
<td>图片</td>
<tdclass="admin_righe_td"><s:filetheme="simple"name="myFile"></s:file></td>
</tr>
注意:
theme=”simple”实现{图片}和上传文件部分不换行。
此外,注意form中,
<formaction="news_add_admin.action" method="post"enctype="multipart/form-data" name="form1"id="form1">
第二步:
Action中:
publicclassNewsActionextends ActionSupport{
private NewsServicenewsService =new NewsService();
private IntegernewsId;
private Newsnews;
privatestaticfinal int BUFFER_SIZE = 1024 * 1024 * 5;
// 实现文件上传
private FilemyFile;// <s:file>标志不仅仅是绑定到myFile
private StringcontentType;// 还有myFileContentType(上传文件的MIME类型)
private StringfileName;// 和myFileFileName(上传文件的文件名,该文件名不包括文件的路径)
private StringimageFileName;
public File getMyFile() {
returnmyFile;
}
publicvoid setMyFile(File myFile){
this.myFile = myFile;
}
public String getMyFileContentType() {
returncontentType;
}
publicvoid setMyFileContentType(StringcontentType) {
this.contentType = contentType;
}
// 页面中使用myFile,Action中也为myFile,注意get和set名字中要有MyFile
public StringgetMyFileFileName() {
returnfileName;
}
publicvoidsetMyFileFileName(String fileName) {
this.fileName = fileName;
}
public StringgetImageFileName() {
returnimageFileName;
}
publicvoid setImageFileName(StringimageFileName) {
this.imageFileName = imageFileName;
}
public NewsServicegetNewsService() {
returnnewsService;
}
publicvoidsetNewsService(NewsService newsService) {
this.newsService = newsService;
}
public Integer getNewsId() {
returnnewsId;
}
publicvoid setNewsId(Integer newsId){
this.newsId = newsId;
}
public News getNews() {
returnnews;
}
publicvoid setNews(News news) {
this.news = news;
}
private Stringurl;
public String getUrl() {
returnurl;
}
publicvoid setUrl(String url) {
this.url = url;
}
public String newslist() {
// Struts2获取request
Maprequest = (Map) ActionContext.getContext().get("request");
request.put("newsList",newsService.list());
// ActionContext.getContext().put("newsList",newsService.list());
returnSUCCESS;
}
public String add() {
imageFileName =new Date().getTime() +getExtention(fileName);//上传后,新文件名
Stringpath = ServletActionContext.getServletContext().getRealPath("/images")+
"/" + imageFileName;
FileimageFile = newFile(path);
copy(myFile, imageFile);
news.setNewsImage("/images"+"/"+imageFileName);
newsService.add(news);
//url
//ActionContext.getContext().put("url","news_newslist_admin.action");
this.setUrl("news_newslist_admin.action");
return"redirect";
}
public String newsadd() {
returnSUCCESS;
}
privatestaticvoid copy(File src, Filedest) {
try {
InputStreamin = null;
OutputStreamout = null;
try {
in= newBufferedInputStream(new FileInputStream(src),BUFFER_SIZE);
out= newBufferedOutputStream(new FileOutputStream(dest),BUFFER_SIZE);
byte[] buffer =newbyte[BUFFER_SIZE];
while (in.read(buffer) >0) {
out.write(buffer);
}
}finally{
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
privatestatic StringgetExtention(String fileName) {
int pos =fileName.lastIndexOf(".");
returnfileName.substring(pos);
}
}
然后在struts.xml文件中进行配置。
小细节:
若在配置文件中使用${url},则需要在相应的Action中,添加url的get和set方法,否则取不到url的值。${ url }在struts.xml中并不是el表达式,只是一种取值方式。
- 使用Struts2标签实现文件上传
- Struts2-文件上传-<s:file>标签使用
- 使用struts2实现多文件上传源码
- 使用struts2如何实现文件上传
- Struts2实现文件上传
- struts2实现文件上传
- struts2实现文件上传
- Struts2实现文件上传
- struts2实现文件上传
- struts2实现文件上传
- struts2实现文件上传
- struts2实现文件上传
- struts2实现文件上传
- Struts2实现文件上传
- Struts2实现文件上传
- Struts2实现文件上传
- Struts2实现文件上传
- struts2实现上传文件
- 曼昆 (第五版)目录
- 整数分解(递归)
- vs2010下,出现链接器工具错误 LNK1104“无法打开文件nafxcw.lib”,或者类似的错误
- 边缘检测(6)Log(Laplacianof Gassian )算子
- Ajax学习
- 使用Struts2标签实现文件上传
- HDU 1170Balloon Comes!
- 黑马程序员----函数,数组
- JAVA中的泛型
- 傅里叶变换的物理意义
- LATEX错误「Unknown graphics extension: .eps」
- 随机产生一个mbit为的长整数
- ASP.NET-FineUI开发实践-1
- RSA加密算法(未完成)