zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    牛客-Python-字符流中第一个不重复的字符
    牛客网-python-表示数值的字符串
    牛客网-(标星)正则表达式的匹配
    牛客网-构建乘积数组
    牛客网-和为S的两个数字
    new code-和为S的连续正数序列
    newcode-平衡二叉树
    Reinforcement Learning学习笔记|Deep Q-learning算法
    Reinforcement Learning学习笔记|Q-learning算法
    C++|重拾|笔记20190307
  • 原文地址:https://www.cnblogs.com/sandyflower/p/3895065.html
Copyright © 2011-2022 走看看