首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

struts2 不要标签文件上传

2013-09-14 
struts2 不用标签文件上传发现网上和书上都是带s:file的有没有不带的例子,请分享下,谢谢struts2[解决办

struts2 不用标签文件上传
发现网上和书上都是带<s:file>的
有没有不带的例子,请分享下,谢谢 struts2
[解决办法]
  如果不用s的标签 那就用input type="file"  做上传一样的
[解决办法]
 网上很多 上传下载啊。自己搜啊。 什么ajax 上传,基于servlet上传。
[解决办法]
不用标签也一样的呀
index.jsp


  <body>
   <form action="uploadAction.action" method="post" enctype="multipart/form-data">
   <input type="file" name="image"><br/>
   <input type="submit" value="上传">
   </form>
   ${message}
  </body>

UploadAction.java

public class UploadAction extends ActionSupport {
private File image;
private String imageFileName;
private String imageContentType;
private String filePath;


public String getFilePath() {
return filePath;
}

public void setFilePath(String filePath) {
this.filePath = filePath;
}

public File getImage() {
return image;
}

public void setImage(File image) {
this.image = image;
}

public String getImageFileName() {
return imageFileName;
}

public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}

public String getImageContentType() {
return imageContentType;
}

public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}

@Override
public String execute() throws Exception {
System.out.println("upload");
String realpath = ServletActionContext.getServletContext().getRealPath(
"/images");
if (this.image != null) {
File savefile = new File(new File(realpath), imageFileName);


if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
this.filePath = "images/" + imageFileName;
ActionContext.getContext().put("message", "文件上传成功");
}
return UploadAction.SUCCESS;
}
}


[解决办法]
<s:form action="upload" enctype="multipart/form-data" method="post">
   <s:textfield name="title" label="文件标题" />
   <s:file name="upload" label="文件选择"/>
   <s:submit value="上传" />
   </s:form>
[解决办法]
其实用不用都一样,具体可以看看页面的input源代码,

http://blog.csdn.net/chenghui0317/article/details/9531879

热点排行