基于struts2的个人信息管理系统(六)

该博客详细介绍了如何使用Struts2框架实现个人文件管理功能,包括查看文件列表、上传文件、查询指定文件、下载文件等操作,涉及到的页面如lookFile.jsp、fileUp.jsp、success.jsp和findFile.jsp,以及对应的业务控制器如FindFileAction和AddFileAction。
摘要由CSDN通过智能技术生成

个人文件管理功能的实现

查看文件列表


上传文件


查询指定文件


下载文件



lookFile.jsp

<%@page import="JavaBean.MyFileBean"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><s:text name="个人信息管理系统->查看"></s:text></title>
    </head>
    <body bgcolor="gray">
        <hr noshade/>
      <s:div align="center">
      <s:form action="findFileAction" method="post">
      <table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
          <tr>
              <td width="33%">
                  <s:a href="http://localhost:8080/ch13/fileManager/fileUp.jsp">上传文件</s:a>
              </td>
              <td width="33%">
                  <s:text name="文件列表"></s:text>
              </td>
              <td width="33%">
                  <s:text name="文件标题:"></s:text>
                  <input type="text" name="title"/>
                  <input type="submit" value="下载"/>
              </td>
          </tr>
      </table>
      </s:form>
      </s:div>
      <hr noshade/>
      <table border="5" cellspacing="0" cellpadding="0" bgcolor="#95BDFF" width="60%" align="center">
          <tr>
              <th height="30">文件标题</th>
              <th height="30">文件名字</th>
              <th height="30">文件类型</th>
              <th height="30">文件大小</th>
          </tr>
          <%
            ArrayList file=(ArrayList)session.getAttribute("file");
            if(file==null||file.size()==0){
            %>
            <s:div align="center"><%="您还没有上传文件!"%></s:div>
            <%
            }else{
                for(int i=file.size()-1;i>=0;i--){
                    MyFileBean ff=(MyFileBean)file.get(i);
                    %> 
                   <tr>
                     <td><%=ff.getTitle()%></td>
                     <td><%=ff.getName()%></td>
                     <td><%=ff.getContentType()%></td>
                     <td><%=ff.getSize()%></td>
                   </tr>
                    <%
                }
            }
          %>
        </table>
    </body>
</html>

lookFile所使用的JavaBean,MyFileBean

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JavaBean;

/**
 *
 * @author abc
 */
public class MyFileBean {
    private String title;
    private String name;
    private String contentType;
    private String size;
    private String filePath;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContentType() {
        return contentType;
    }
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
    public String getSize() {
        return size;
    }
    public void setSize(String size) {
        this.size = size;
    }
    public String getFilePath() {
        return filePath;
    }
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

lookFile.jsp所对应的业务控制器FindFileAction

package edu.fileManager.Action;

import DBJavaBean.DB;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import javax.swing.JOptionPane;
import org.apache.struts2.interceptor.ServletRequestAware;

public class FindFileAction extends ActionSupport implements ServletRequestAware{
    private String title;
    private String userName;
    private ResultSet rs=null;
    private String message=ERROR;
    private HttpServletRequest request;
        public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public void message(String msg){
        int type=JOptionPane.YES_NO_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null,msg,title,type);
    }
    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        request=hsr;
    }
    @Override
    public void validate(){
        if(this.getTitle()==null||this.getTitle().length()==0){
            message("文件标题不允许为空!");
            addFieldError("title","文件标题不允许为空!");
        }else{
            try{
                DB mysql=new DB();
                userName=mysql.returnLogin(request);
                rs=mysql.selectFile(request, userName, "title", this.getTitle());
                if(!rs.next()){
                    message("此文件标题不存在!");
                    addFieldError("title","此文件标题不存在!");
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    @Override
    public String execute() throws Exception {
        DB mysql=new DB();
        userName=mysql.returnLogin(request);
        String file=mysql.findFile(request, userName, this.getTitle());
        if(file.equals("ok")){
            message=SUCCESS;
        }
        return message;
    }   
}

fileUp.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><s:text name="个人信息管理系统->文件上传"></s:text></title>
    </head>
    <body bgcolor="gray">
        <hr noshade/>
      <s:div align="center">
      <s:form action="findFileAction" method="post">
      <table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
          <tr>
              <td width="33%">
                  <s:text name="上传文件"></s:text>
              </td>
              <td width="33%">
                  <s:a href="http://localhost:8080/ch13/fileManager/lookFile.jsp">文件列表</s:a>
              </td>
              <td width="33%">
                  <s:text name="文件标题:"></s:text>
                  <input type="text" name="title"/>
                  <input type="submit" value="下载"/>
              </td>
          </tr>
      </table>
      </s:form>
      </s:div>
      <hr noshade/>
      <s:form action="addFileAction" method="post" enctype="multipart/form-data">
          <table border="5" cellspacing="0" cellpadding="0" bgcolor="#95BDFF" width="60%" align="center">
                <tr>
                     <td>
                         <s:textfield name="title" label="文件标题" size="30"></s:textfield>
                     </td>
                </tr>
                <tr>
                     <td height="30">
                         <s:file name="upload" label="选择文件" size="30"></s:file>
                     </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="submit" value="上 传" size="12"/>     
                        <input type="reset" value="清 空" size="12"/>
                    </td>
                </tr>
            </table>
        </s:form>
    </body>
</html>

所对应的业务控制器AddFileAction

package edu.fileManager.Action;

import DBJavaBean.DB;
import com.opensymphony.xwork2.ActionSupport;
import java.io.*;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.text.DecimalFormat;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import javax.swing.JOptionPane;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

public class AddFileAction extends ActionSupport implements ServletRequestAware{
    private String title;
    private File upload;
    private String uploadContentType;
    private String uploadFileName;
    private String savePath;
    private String userName;
    private String message=ERROR;
    private HttpServletRequest request;
    private ResultSet rs;
    public String getTitle() {
        return this.title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public File getUpload() {
        return this.upload;
    }
    public void setUpload(File upload) {
        this.upload = upload;
    }
    public String getUploadContentType() {
        return this.uploadContentType;
    }
    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }
    public String getUploadFileName() {
        return this.uploadFileName;
    }
    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
    public String getSavePath(){
        String Path=null;
        try{
        Path=ServletActionContext.getServletContext().getRealPath(this.savePath);
        }catch(Exception e){
            message("异常:"+e);
            e.printStackTrace();
        }
        return Path;
    }
    public void setSavePath(String value) {
        this.savePath = value;
    }

    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        request=hsr;
    }
    public String Path(String Path){
        String filePath="";
        String p="";
        StringTokenizer token=new StringTokenizer(Path,"\\");
        while(!(p.equals("save"))){
            p=token.nextToken();
            filePath=filePath+p+"/";
        }
        return filePath;
    }
    public void message(String msg){
        int type=JOptionPane.YES_NO_OPTION;
        String title2="信息提示";
        JOptionPane.showMessageDialog(null,msg,title2,type);
    }
    /*public void validate(){
        if(this.getTitle()==null||this.getTitle().length()==0){
            addFieldError("title","文件标题不允许为空!");
        }
        if(this.getUpload()==null||this.getUpload().length()==0){
            addFieldError("upload","请选择一个文件!");
        }else{
                try{
                    DB mysql=new DB();
                    userName=mysql.returnLogin(request);
                    rs=mysql.selectFile(request, userName, "title", this.getTitle());
                    if(rs.next()){
                        addFieldError("title","文件标题已存在!");
                    }else{
                        rs=mysql.selectFile(request, userName, "name", this.getUploadFileName());
                        if(rs.next()){
                            addFieldError("upload","文件名已存在!");
                        }
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
        }
    }*/
    public String execute() throws Exception{
        FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
        FileInputStream fis=new FileInputStream(getUpload());
        byte[] buffer=new byte[1024];
        int len=0;
        while((len=fis.read(buffer))>0){
            fos.write(buffer,0,len);
        }
        fos.close();
        DB mysql=new DB();
        userName=mysql.returnLogin(request);
        String size=null;
        DecimalFormat dcmFmt = new DecimalFormat("0.00");
        float length=this.getUpload().length();
        if(length<1024){
            size=(dcmFmt.format(length))+"字节";
        }else if(length<1024*1024){
            size=(dcmFmt.format(length/1024))+"K";
        }else{
            size=(dcmFmt.format(length/1024*1024))+"M";
        }
        String filePath=Path(this.getSavePath())+this.getUploadFileName();
        System.out.println("dsdsadaasdasdasdasdasdsaasdassdasd");
        System.out.println(filePath);
        String file=mysql.insertFile(request, userName, this.getTitle(), this.getUploadFileName(), this.getUploadContentType(), size, filePath);
        System.out.println(file);
        if(file.equals("ok")){
            message=SUCCESS;
        }else if(file.equals("title")){
            message=INPUT;
        }else if(file.equals("name")){
            message=INPUT;
        }
        return message;
    }  
}


上传成功页面success.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><s:text name="文件上传成功"></s:text></title>
        <META HTTP-EQUIV="Refresh" CONTENT="3; URL=http://localhost:8080/ch13/fileManager/lookFile.jsp"/>
    </head>
    <body bgcolor="gray">
        <s:div align="center">
            <h1>文件上传成功!</h1>
            <s:a href="http://localhost:8080/ch13/fileManager/lookFile.jsp"><h3>3秒后自动跳转......</h3></s:a>
            <hr/>
            文件标题:<s:property value="+title"/><br/>
            <s:property value="uploadFileName"/><br/>
            <image src="<s:property value="'../save/'+uploadFileName"/>"/>
        </s:div>     
    </body>
</html>

findFile.jsp

<%@page import="JavaBean.MyFileBean"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><s:text name="个人信息管理系统->查找"></s:text></title>
    </head>
    <body bgcolor="gray">
        <hr noshade/>
      <s:div align="center">
      <s:form action="findFileAction" method="post">
      <table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
          <tr>
              <td width="33%">
                  <s:a href="http://localhost:8080/ch13/fileManager/fileUp.jsp">上传文件</s:a>
              </td>
              <td width="33%">
                  <s:a href="http://localhost:8080/ch13/fileManager/lookFile.jsp">文件列表</s:a>
              </td>
              <td width="33%">
                  <s:text name="文件标题:"></s:text>
                  <input type="text" name="title"/>
                  <input type="submit" value="下载"/>
              </td>
          </tr>
      </table>
      </s:form>
      </s:div>
      <hr noshade/>
      <table border="5" cellspacing="0" cellpadding="0" bgcolor="#95BDFF" width="60%" align="center">
          <tr>
              <th height="30">文件标题</th>
              <th height="30">文件名字</th>
              <th height="30">文件类型</th>
              <th height="30">文件大小</th>
              <th height="30">用户操作</th>
          </tr>
          <%
            ArrayList file=(ArrayList)session.getAttribute("findfile");
            if(file==null||file.size()==0){
            %>
            <s:div align="center"><%="您还没有上传文件!"%></s:div>
            <%
            }else{
                for(int i=file.size()-1;i>=0;i--){
                    MyFileBean ff=(MyFileBean)file.get(i);
                    %> 
                   <tr>
                     <td><%=ff.getTitle()%></td>
                     <td><%=ff.getName()%></td>
                     <td><%=ff.getContentType()%></td>
                     <td><%=ff.getSize()%></td>
                     <td>
                         <s:a href="downFileAction">下载</s:a>
                         <s:a href="deleteFileAction">删除</s:a>
                     </td>
                   </tr>
                   <tr align="center">
                       <td colspan="5">
                           <img src="../save/<%=ff.getName()%>"/>
                       </td>
                   </tr>
                    <%
                }
            }
          %>
        </table>
    </body>
</html>

最后删除,所用的业务控制器DeleteFileAction

package edu.fileManager.Action;

import DBJavaBean.DB;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;

public class DeleteFileAction extends ActionSupport implements ServletRequestAware{
    private String message=ERROR;
    private String userName;
    private String title;
    private String filePath;
    private HttpServletRequest request;
    
    public String DeleteFile(String FilePath){
        try{
            String filePath=FilePath;
            filePath=filePath.toString();
            File deleFile=new File(filePath);
            deleFile.delete();
            return "ok";
        }catch(Exception e){
            return null;
        }
    }
    public void setServletRequest(HttpServletRequest hsr) {
        request=hsr;
    }
    public String execute() throws Exception {
        DB mysql=new DB();
        userName=mysql.returnLogin(request);
        title=mysql.returnFile(request,"title");
        filePath=mysql.returnFile(request, "filePath");
        String tit=mysql.deleteFile(request, userName, title);
        if(tit.equals("ok")){
            String del=DeleteFile(filePath);
            if(del.equals("ok")){
                message=SUCCESS;
            }
        }
        return message;
    }
}
项目源码
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值