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>
  • 相关阅读:
    苹果IPhone真机开发调试
    Unity3d 错误提示 GUI Error: You are pushing more GUIClips than you are popping. Make sure they are balanced
    Unity编辑器环境在Inspector面板中显示变量
    微信小程序开发
    Android 9.0 Http不能访问网络
    c#传不确定的参数个数,比如int型
    玩转@Git三剑客
    白话法律42讲-为程序员打造的专属法律武器
    uSurvival 1.41多人在线生存逃杀吃鸡类游戏源码
    NGUI: Next-Gen UI 2018.3.0f
  • 原文地址:https://www.cnblogs.com/sandyflower/p/3895065.html
Copyright © 2011-2022 走看看