文件模块呢。。首先是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应该是把各种数据封装起来。利于传输数据。