求struts2上传文件代码,最好详细些。新手
急,求上传例子,百度那些我都看过,有没有真实的例子。 --------------------编程问答-------------------- --------------------编程问答--------------------马上能给你写一个简单的例子,^_^ --------------------编程问答-------------------- struts1.2上传
你自己参照着改成 s2 的吧 --------------------编程问答-------------------- action:
//文件上传下载相关属性
private File file;
private String filename;
private String fileFileName;
private String fileContentType;
private String savePath;
//上传文件
public String upload() throws Exception{
// 建立文件输出流
String path=ServletActionContext.getServletContext().getRealPath(getSavePath());
FileOutputStream fos = new FileOutputStream(path+ "\\" + getFileFileName());
// 建立文件上传流
FileInputStream fis = new FileInputStream(getFile());
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
struts.xml:
<!--系统支持的最大上传文件 -->--------------------编程问答-------------------- http://blog.csdn.net/withiter/article/details/7272338 --------------------编程问答-------------------- 我这里有个demo,要的话,一手给分,一手交货 --------------------编程问答-------------------- 给你一个更好的例子,在我的博客里,Struts实现异步上传下载,很详细,你可以去看一下:http://blog.csdn.net/harderxin/article/details/9022439 --------------------编程问答--------------------
<constant name="struts.multipart.maxSize" value="100000000"/>
<action name="upload" class="com.oa.action.DocumentAction">
<param name="savePath">upload</param>
<!--配置上传易做图 -->
<interceptor-ref name="fileUpload">
<!-- 限制上传的文件类型 -->
<paramname="allowedTypes">application/msword,application/zip,application/vnd.ms-excel,text/plain,application/pdf,application/vnd.ms-powerpoint</param>
<!--限制文件上传大小,最大为5M -->
<param name="maximumSize">5242880</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<result>/Document/Upload.jsp</result>
<result name="input">/Document/Upload.jsp</result>
</action>
package com.zsq.sful.action;
import java.io.File;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.faces.application.ApplicationAssociate;
public class UpLoadAction extends ActionSupport{
private File image;
private String imageFileName;
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 execute()throws Exception{
if(!(image==null)){
String filename=ServletActionContext.getServletContext().getRealPath("/image");
File savePath = new File(new File(filename), imageFileName);
if(!savePath.exists())savePath.getParentFile().mkdirs();
FileUtils.copyFile(image, savePath);
System.out.println(filename);
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
}
具体的action。。。在页面上 只写一个 文本框即可,
jsp页面:
<body>
<form enctype="multipart/form-data" action="UpLoad" method="post">
文件:<input type="file" name="image">
<input type="submit" value="上传"/>
</form>
</body>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="admin" namespace="/" extends="struts-default">
<global-results >
<result>afterupload.jsp</result>
</global-results>
<action name="UpLoad" class="com.zsq.sful.action.UpLoadAction"></action>
</package>
</struts>
自己写一个afterupload.jsp页面。。。
--------------------编程问答-------------------- 好坑。。。 --------------------编程问答--------------------
够详细,谢了 --------------------编程问答-------------------- 学习了。以前一直在富文本编辑器里面上传附件,,, --------------------编程问答--------------------
补充:Java , Web 开发