首页 > 代码库 > strus 上传文件

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>