蜗牛—JavaWeb之个人信息管理系统(五)

文件模块呢。。首先是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/ch04/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>
AddFileAction.java

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="ERRROE";
    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();
        String file=mysql.insertFile(request, userName, this.getTitle(), this.getUploadFileName(), this.getUploadContentType(), size, filePath);
        if(file.equals("ok")){
            message="SUCCESS";
        }else if(file.equals("title")){
            message="input";
        }else if(file.equals("name")){
            message="input";
        }
        return message;
    }  
}

上传文件时的页面.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/ch04/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>
查找文件的Action.java

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;
    }   
}
查找文件的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/ch04/fileManager/fileUp.jsp">上传文件</s:a>
              </td>
              <td width="33%">
                  <s:a href="http://localhost:8080/ch04/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>
删除文件时的Action.java]

package edu.fileManager.Action;

import DBJavaBean.DB;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.swing.JOptionPane;
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;
    }
}
下载时的Action.java

package edu.fileManager.Action;

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

public class DownFileAction extends ActionSupport implements ServletRequestAware{
    private String message="ERROR";
    private HttpServletRequest request;
    public String getDownloadFileName() {
       DB mysql=new DB();
       String downFileName = mysql.returnFile(request, "fileName");
       try {
        downFileName = new String(downFileName.getBytes(), "ISO8859-1");
       } catch (Exception e) {
        e.printStackTrace();
       }
       return downFileName;
    }
    public InputStream getInputStream() throws Exception{
        DB mysql=new DB();
        String downFileName = mysql.returnFile(request, "fileName");
        String path="/YouMessage/save/"+downFileName;
        return ServletActionContext.getServletContext().getResourceAsStream(path);
    }
    public void setServletRequest(HttpServletRequest hsr) {
        request=hsr;
    }
    public String execute() throws Exception{
        message="SUCCESS";
        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/ch04/fileManager/lookFile.jsp"/>
    </head>
    <body bgcolor="gray">
        <s:div align="center">
            <h1>文件上传成功!</h1>
            <s:a href="http://localhost:8080/ch04/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>

这样,基本上四个模块功能实现了。

下面,五个基本JavaBean文件。JavaBean应该是把各种数据封装起来。利于传输数据。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在日常办公中有许多常用的个人数据,如朋友电话、邮件地址、日程安排、日常记事、文件上传和下载,这些都可以用一个个人信息管理系统进行管理。个人信息管理系统可以内置于手掌上的数字处理器,以提供电子名片、便条、行程管理等功能。本项目基于B/S设计,也可以发布到网上,用户可以随时存取个人信息。 用户可以在系统中任意添加、修改、删除个人数据,包括个人的基本信息、个人通讯录、日程安排、个人文件管理。 要实现的功能包括四个方面: (1)登录与注册 系统的登录和注册功能。 (2)个人基本信息管理模块 系统中对个人基本信息的管理包括:个人的姓名、性别、出生日期、民族、学历、职称、登录名、密码、电话、家庭住址等。 (2)用户个人通讯录模块 系统的个人通讯录是保存了个人的通讯录信息,包括自己联系人的姓名、电话、邮箱、工作单位、地址、QQ等。可以自由添加联系人的信息,查询或删除联系人。 (3)日程安排模块 日程模块记录自己的活动安排或者其它有关事项,如添加从某一时间到另一时间要做什么事,日程标题、内容、开始时间、结束时间。可以自由查询,修改,删除。 (4)个人文件管理模块 该模块实现用户在网上存储临时文件的功能。用户可以新建文件夹,修改、删除、移动文件夹;上传文件、修改文件名、下载文件、删除文件、移动文件等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值