一.只能上传txt文件和下载指定文件
二.结构图
三.源代码
- Servlet
DownLoad.java
(原来可以的,但是后来放在包里面就不知怎么访问不了,下面直接在html里面的超链接使用download属性,此Servlet可不要)
package com.DownLoad;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DownLoad
*/
@WebServlet("/DownLoad")
public class DownLoad extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DownLoad() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String fileName = "Document_description.txt";
byte[] bytes = fileName.getBytes("utf-8");
fileName = new String(bytes, "ISO8859-1");
//修改响应的头部属性content-disposition值为attachment
resp.setHeader("content-disposition", "attachment;filename=" + fileName );
//获取连接服务器端资源文件的输入流
InputStream is = this.getServletContext().getResourceAsStream("/Document_description.txt");
//获取输出流
ServletOutputStream os = resp.getOutputStream();
//将输入流中的数据写入到输出流中
int len = -1;
byte[] buf = new byte[1024];
while((len = is.read(buf)) != -1){
os.write(buf, 0 , len);
}
os.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
UpLoad.java
package com.UpLoad;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class UpLoad
*/
@WebServlet("/UpLoad")
public class UpLoad extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UpLoad() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("dopost......");
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
InputStream is= request.getInputStream();//通过获取输入流对象
String savepath = "D:\\文件"; //设置文件的保存路径
File f = new File(savepath); //创建一个文件对象
if(!f.exists()){ //判断这个文件夹是否存在
f.mkdir(); //如果不存在创建这个文件夹
}
FileOutputStream fos = new FileOutputStream(savepath+"\\"+"file.txt"); //创建一个文件输出流对象
byte []b = new byte[1024]; //开辟一个缓存空间
int len=0;
while((len=is.read(b))!=-1) //读取数据
{
fos.write(b, 0, len); //写入数据(也就是写入到D:\文件\file.txt,这是我们前面设置的路径)
}
fos.close();//关闭数据流
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>文件上传</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<h1 align='center'>上传成功!</h1>");
out.println("<center><input type='button' name='Submit' οnclick='javascript:history.back(-1);' value='返回上一页'><center>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
-data
1.Document_description.txt //下载的源文件
2.User.txt //用户注册的账号信息
- jsp
error.jsp
错误页面处理
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>出错页面</title>
<style>
body{background-image:url('../image/error3.jpg');font-family: "微软雅黑", sans-serif;}
</style>
</head>
<body>
<h1 align="center">服务器开小差啦!!!</h1>
<%=exception.toString()%>
</body>
</html>
Login.jsp
登录页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page errorPage="error.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8">
<title>登录界面</title>
<style>
/* 让页面所有元素的padding和margin都设置为0 */
*{margin:0;padding:0;box-sizing:border-box;}
/*字体设置为微软雅黑 */
body{background-image:url('../image/bg.jpg');font-family: "微软雅黑", sans-serif;}
/* 引用图片高度设置为28px,就是页面最上方像屋檐一样的黑色图 */
.headtop{height:28px;}
/* 整个登录框的css,并使用绝对定位居中 */
.login {
position: absolute;
top: 50%;
left: 50%;
margin: -150px 0 0 -150px;
width:300px;
height:300px;
}
/* 前面分析过的h1标签的css,text-shadow设置阴影使文字更好看,letter-spacing设置字符间距 */
.login h1 { color:#555555; text-shadow: 0px 10px 8px #CDC673; letter-spacing:2px;text-align:center;margin-bottom:20px; }
/* 两个输入框的css,border属性设置边框线粗细以及颜色,border-radius设置边框的圆角角度 */
input{
padding:10px;
width:100%;
color:white;
margin-bottom:10px;
background-color:#555555;
border: 1px solid black;
border-radius:4px;
letter-spacing:2px;
}
/* 登录按钮的css,cursor:pointer当鼠标移到按钮上面时变成小手形状 */
button{
width:100%;
padding:10px;
background-color:#CDC673;
border:1px solid black;
border-radius:4px;
cursor:pointer;
}
</style>
</head>
<body>
<div class="headtop"></div>
<div class="login">
<h1>登录</h1>
<form action="Log-submit.jsp" method="post">
<input type="text" name="username" placeholder="用户名" required="required">
<input type="password" name="password" placeholder="密 码" required="required">
<button type="submit">登录</button>
</form>
<button onclick="window.location.href='./Register.jsp'">前往注册</button>
</div>
</body>
</html>
Log-submit.jsp
登录提交页面
<%@ page contentType="text/html; charset=utf-8" language="java"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.ArrayList"%>
<%@ page errorPage="../error_jsp/error.jsp" %>
<html>
<head>
<meta charset="utf-8">
<title>登录结果</title>
<style>
/* 让页面所有元素的padding和margin都设置为0 */
*{margin:0;padding:0;box-sizing:border-box;}
/*字体设置为微软雅黑 */
body{background-image:url('../image/bg.jpg');font-family: "微软雅黑", sans-serif;}
/* 引用图片高度设置为28px,就是页面最上方像屋檐一样的黑色图 */
.headtop{height:28px;}
/* 整个登录框的css,并使用绝对定位居中 */
.login {
position: absolute;
top: 50%;
left: 50%;
margin: -150px 0 0 -150px;
width:300px;
height:300px;
}
/* 前面分析过的h1标签的css,text-shadow设置阴影使文字更好看,letter-spacing设置字符间距 */
.login h1 { color:#555555; text-shadow: 0px 10px 8px #CDC673; letter-spacing:2px;text-align:center;margin-bottom:20px; }
/* 两个输入框的css,border属性设置边框线粗细以及颜色,border-radius设置边框的圆角角度 */
input{
padding:10px;
width:20%;
color:white;
margin-bottom:10px;
background-color:#555555;
border: 1px solid black;
border-radius:4px;
letter-spacing:2px;
}
/* 登录按钮的css,cursor:pointer当鼠标移到按钮上面时变成小手形状 */
button{
width:20%;
padding:10px;
background-color:#CDC673;
border:1px solid black;
border-radius:4px;
cursor:pointer;
}
</style>
</head>
<body>
<div class="container">
<%String username = request.getParameter("username");
String password = request.getParameter("password");
%>
<%
boolean flag = false;
// 使用字符缓冲输入流读取User.txt文件中的内容
BufferedReader br = null;
br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\workspace\\201741404234LiTianci\\WebContent\\data\\User.txt"));
// 一次读取一行
String line = null;
while ((line = br.readLine()) != null) {
// 字符串的分割功能
String[] datas = line.split(":");
// 判断
if (datas[0].equals(username))
{ if(datas[1].equals(password))
{
flag = true;
break;
}
else
{
out.println("<center><button οnclick=\"window.location.href='./Login.jsp'\">重新登录</button></center>");
out.println("<script language='javascript'>alert('密码错误,请重新输入')</script>");
break;
}
}
}
if(flag==true)
{
out.println("<h1 align='center'>"+username+"您已经成功登录系统"+"<h1>");
String promt=new String();
String Name=username;
boolean hasLog=false;
ArrayList names=(ArrayList)session.getAttribute("lognames");
if(names==null)
{
names=new ArrayList();
names.add(Name);
session.setAttribute("lognames",names);
out.println("欢迎登录!这是你首次登录,你的名字已经写入session");
}
else
{
for(int i=0;i<names.size();i++)
{
String temp=(String)names.get(i);
if(temp.equals(username))
{
out.println("你已经登录过了,可以进行下面的操作!");
hasLog=true;
break;
}
}
%>
<center>
<a href="Login.jsp"><button>切换用户</button></a>
</center>
<form action="/201741404234LiTianci/UpLoad" enctype="multipart/form-data" method="post">
上传文件
<br>
<input id="myfile" type="file" name="myfile" size=30 value="选择文件"/>
<input type="submit" name="upload" value="上传"/>
</form>
软件说明文档下载:<a href="../data/Document_description.txt" download="Document_description.txt">Document_description.txt</a>
<%
if(!hasLog)
{
names.add(Name);
session.setAttribute("lognames",names);
out.println("欢迎登录!这是你首次登录,你的名字已经写入session");
}
}
}
if(line==null)
{
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<br/>");
out.println("<center><button οnclick=\"window.location.href='./Login.jsp'\">重新登录</button></center>");
out.println("<center><button οnclick=\"window.location.href='../Reg_jsp/Register.jsp'\">前往注册</button></center>");
out.println("<script language='javascript'>alert('用户不存在,请重新登录或者前往注册')</script>");
}
%>
</div>
Register.jsp
注册页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page errorPage="error.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册界面</title>
<style>
/* 让页面所有元素的padding和margin都设置为0 */
*{margin:0;padding:0;box-sizing:border-box;}
/* 字体设置为微软雅黑 */
body{background-image:url('../image/bg2.jpg');font-family: "微软雅黑", sans-serif;}
/* 引用图片高度设置为28px,就是页面最上方像屋檐一样的黑色图 */
.headtop{height:28px;}
/* 整个登录框的css,并使用绝对定位居中 */
.register {
position: absolute;
top: 50%;
left: 50%;
margin: -150px 0 0 -150px;
width:300px;
height:300px;
}
/* 前面分析过的h1标签的css,text-shadow设置阴影使文字更好看,letter-spacing设置字符间距 */
.register h1 { color:#555555; text-shadow: 0px 10px 8px #CDC673; letter-spacing:2px;text-align:center;margin-bottom:20px; }
/* 两个输入框的css,border属性设置边框线粗细以及颜色,border-radius设置边框的圆角角度 */
input{
padding:10px;
width:100%;
color:white;
margin-bottom:10px;
background-color:#555555;
border: 1px solid black;
border-radius:4px;
letter-spacing:2px;
}
/* 登录按钮的css,cursor:pointer当鼠标移到按钮上面时变成小手形状 */
form button{
width:100%;
padding:10px;
background-color:#CDC673;
border:1px solid black;
border-radius:4px;
cursor:pointer;
}
</style>
</head>
<body>
<div class="headtop"></div>
<div class="register">
<h1>注册</h1>
<form action="Reg-submit.jsp" method="post">
<input type="text" name="username" placeholder="用户名" required="required">
<input type="password" name="password" placeholder="密 码" required="required">
<input type="password" name="qrpassword" placeholder="确认密码" required="required">
<button type="submit">注册</button>
<button onclick="window.location.href='./Login.jsp'">返回登录</button>
</form>
</div>
</body>
</html>
Reg-submit.jsp
注册提交页面
<%@ page contentType="text/html; charset=utf-8" language="java"%>
<%@ page import="java.io.*"%>
<%@ page errorPage="../error_jsp/error.jsp" %>
<html>
<head>
<meta charset="utf-8">
<title>注册结果</title>
<style>
/* 让页面所有元素的padding和margin都设置为0 */
*{margin:0;padding:0;box-sizing:border-box;}
/*字体设置为微软雅黑 */
body{background-image:url('../image/bg.jpg');font-family: "微软雅黑", sans-serif;}
/* 引用图片高度设置为28px,就是页面最上方像屋檐一样的黑色图 */
.headtop{height:28px;}
/* 整个登录框的css,并使用绝对定位居中 */
.login {
position: absolute;
top: 50%;
left: 50%;
margin: -150px 0 0 -150px;
width:300px;
height:300px;
}
/* 前面分析过的h1标签的css,text-shadow设置阴影使文字更好看,letter-spacing设置字符间距 */
.login h1 { color:#555555; text-shadow: 0px 10px 8px #CDC673; letter-spacing:2px;text-align:center;margin-bottom:20px; }
/* 两个输入框的css,border属性设置边框线粗细以及颜色,border-radius设置边框的圆角角度 */
input{
padding:10px;
width:100%;
color:white;
margin-bottom:10px;
background-color:#555555;
border: 1px solid black;
border-radius:4px;
letter-spacing:2px;
}
/* 登录按钮的css,cursor:pointer当鼠标移到按钮上面时变成小手形状 */
button{
width:20%;
padding:10px;
background-color:#CDC673;
border:1px solid black;
border-radius:4px;
cursor:pointer;
}
</style>
</head>
<body>
<div class="container">
<% BufferedReader br = null;
br = new BufferedReader(new FileReader("C:\\Users\\Administrator\\workspace\\201741404234LiTianci\\WebContent\\data\\User.txt"));
String line = null;
boolean flag = true;
%>
<%String username = request.getParameter("username");
String password = request.getParameter("password");
String qrpassword = request.getParameter("qrpassword");
%>
<%
while((line=br.readLine())!=null)
{
String[] datas = line.split(":");
if (datas[0].equals(request.getParameter("username")))
{
flag=false;
out.println("<script language='javascript'>alert('用户名已经存在,请重新输入')</script>");
out.println("<center><button οnclick=\"window.location.href='./Register.jsp'\">重新注册</button></center>");
break;
}
}
if(flag)
{
if(password.equals(qrpassword)){
out.println("<div style=\"width:600px;margin-left:auto;margin-right:auto;\">注册成功");
out.println("<ul>");
out.println("<li>用户名:" + username + "</li>");
out.println("<li>密码:" + password + "</li>");
out.println("</ul></div>");
//打开文件
FileWriter f=new FileWriter("C:\\Users\\Administrator\\workspace\\201741404234LiTianci\\WebContent\\data\\User.txt",true);
BufferedWriter bw=new BufferedWriter(f);
String info=request.getParameter("username")+":"+request.getParameter("password");
//在文件中写入数据
bw.write(info);
bw.newLine();
bw.flush();
//关闭文件流
bw.close();
out.println("<center><button οnclick=\"window.location.href='./Login.jsp'\">注册成功,返回登录</button></center>");
}
else{
out.println("<script language='javascript'>alert('两次密码不一致,请重新输入!')</script>");
out.println("<center><button οnclick=\"window.location.href='./Register.jsp'\">返回注册</button></center>");
}
}
%>
<!-- <%=username %>
<%=password %>
<%=qrpassword %> -->
</div>
-image
放自己喜欢的照片就好
四.运行结果
登录模块
登录页面
登录用户不存在
首次登录
重新登录
注册模块
注册用户已存在
两次密码不一致
注册成功
文件上传下载
文件上传下载页面
文件上传成功
文件下载