jsp+javaBean 网站附件管理系统(未完成版)-3

处理附件上传的JSP页面与JAVA Bean 

 

<% @ page language = " java "  contentType = " text/html; charset=GB18030 "
    pageEncoding
= " GB18030 " %>
<! 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=GB18030 " >
< jsp:useBean id = " FUpload "  scope = " page "   class = " edu.scnu.crjy.common.UploadFiles " ></ jsp:useBean >

</ head >


< body >

<%
    
    String documentRoot 
=  getServletContext().getRealPath( " / " );    
    String Dir 
=  documentRoot + " appendfiles// " + request.getParameter( " childDir " ); 
    out.println(request.getParameter(
" childDir " ));
    
// 保存文件的路径,请确保目录存在,或改到其他目录 
    
// 通过调用JavaBeans的方法完成上传操作 
    FUpload.setUploadDirectory(Dir);
    FUpload.uploadFile(request);

%>
< script language = " javascript " >
    alert(
" 上传附件成功! " );
    document.location.href
= " ../appendfileslist.jsp?childDir=<%=request.getParameter( " childDir " )%> "
</ script >
</ body >
</ html >

注:这个处理上传的bean有大部分是从csdn中获得的资料

package  edu.scnu.crjy.common;

import  java.io. * ;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.ServletInputStream;
import  javax.servlet.ServletException;

public   class  UploadFiles  {
    
private static String newline = " "// 设定换行符

    
private String uploadDirectory = "."// 默认的保存位置

    
private String ContentType = ""// 文件类型

    
private String CharacterEncoding = ""// 编码格式

    
// 设定文件要保存在服务器中的位置
    public void setUploadDirectory(String s) {
        uploadDirectory 
= s;
    }


    
// 提取文件名,本方法是把用户上传的文件按照原名保存
    private String getFileName(String s) {
        
int i = s.lastIndexOf("/");
        
if (i < 0 || i >= s.length() - 1{
            i 
= s.lastIndexOf("/");
            
if (i < 0 || i >= s.length() - 1)
                
return s;
        }

        
return s.substring(i + 1);
    }


    
// 得到content-type
    public void setContentType(String s) {
        ContentType 
= s;
        
int j;
        
if ((j = ContentType.indexOf("boundary=")) != -1{
            ContentType 
= ContentType.substring(j + 9);
            ContentType 
= "--" + ContentType;
        }

    }


    
// 得到文件编码格式
    public void setCharacterEncoding(String s) {
        CharacterEncoding 
= s;
    }


    
public void uploadFile(HttpServletRequest req) throws ServletException,
            IOException 
{
        setCharacterEncoding(req.getCharacterEncoding());
        setContentType(req.getContentType());
        uploadFile(req.getInputStream());
    }


    
public void uploadFile(ServletInputStream servletinputstream)
            
throws ServletException, IOException {

        String s5 
= null;
        String filename 
= null;
        
byte Linebyte[] = new byte[4096];
        
byte outLinebyte[] = new byte[4096];
        
int ai[] = new int[1];
        
int ai1[] = new int[1];

        String line;
        
// 得到文件名
        while ((line = readLine(Linebyte, ai, servletinputstream,
                CharacterEncoding)) 
!= null{
            
int i = line.indexOf("filename=");
            
if (i >= 0{
                line 
= line.substring(i + 10);
                
if ((i = line.indexOf(""")) > 0)
                    line 
= line.substring(0, i);
                
break;
            }

        }


        filename 
= line;
        
if (filename != null && !filename.equals(""")) {
            filename 
= getFileName(filename);

            String sContentType 
= readLine(Linebyte, ai, servletinputstream,
                    CharacterEncoding);
            
if (sContentType.indexOf("Content-Type">= 0)
                readLine(Linebyte, ai, servletinputstream, CharacterEncoding);

            
// 建立新文件的文件句柄
            File file = new File(uploadDirectory, filename);

            
// 建立生成新文件的输出流
            FileOutputStream fileoutputstream = new FileOutputStream(file);

            
while ((sContentType = readLine(Linebyte, ai, servletinputstream,
                    CharacterEncoding)) 
!= null{
                
if (sContentType.indexOf(ContentType) == 0 && Linebyte[0== 45)
                    
break;

                
if (s5 != null{
                    
// 写入新文件
                    fileoutputstream.write(outLinebyte, 0, ai1[0]);
                    fileoutputstream.flush();
                }

                s5 
= readLine(outLinebyte, ai1, servletinputstream,
                        CharacterEncoding);
                
if (s5 == null || s5.indexOf(ContentType) == 0
                        
&& outLinebyte[0== 45)
                    
break;
                fileoutputstream.write(Linebyte, 
0, ai[0]);
                fileoutputstream.flush();
            }


            
byte byte0;
            
if (newline.length() == 1)
                byte0 
= 2;
            
else
                byte0 
= 1;
            
if (s5 != null && outLinebyte[0!= 45
                    
&& ai1[0> newline.length() * byte0)
                fileoutputstream.write(outLinebyte, 
0, ai1[0]
                        
- newline.length() * byte0);
            
if (sContentType != null && Linebyte[0!= 45
                    
&& ai[0> newline.length() * byte0)
                fileoutputstream.write(Linebyte, 
0, ai[0- newline.length()
                        
* byte0);
            fileoutputstream.close();
        }

    }


    
// readLine函数把表单提交上来的数据按行读取
    private String readLine(byte Linebyte[], int ai[],
            ServletInputStream servletinputstream, String CharacterEncoding) 
{
        
try // 读取一行
            ai[0= servletinputstream.readLine(Linebyte, 0, Linebyte.length);
            
if (ai[0== -1)
                
return null;
        }
 catch (IOException ex) {
            
return null;
        }

        
try {
            
if (CharacterEncoding == null{
                
// 用默认的编码方式把给定的byte数组转换为字符串
                return new String(Linebyte, 0, ai[0]);
            }
 else {
                
// 用给定的编码方式把给定的byte数组转换为字符串
                return new String(Linebyte, 0, ai[0], CharacterEncoding);
            }

        }
 catch (Exception ex) {
            
return null;
        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的JSP+JavaBean大学日记管理系统的代码模,供你参考: 1. 创建数据库和表 首先,需要创建一个名为diary的数据库,然后在该数据库中创建一个名为user_diary的表,该表包含以下字段: - id:日记ID,自增长 - user_id:用户ID - title:日记标题 - content:日记内容 - create_time:日记创建时间 可以使用以下SQL语句创建user_diary表: ``` CREATE TABLE `user_diary` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `title` varchar(255) NOT NULL, `content` text NOT NULL, `create_time` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; ``` 2. 创建JavaBean 在src目录下创建一个名为Diary.javaJavaBean,该类用于封装日记的信息,包括id、user_id、title、content、create_time等属性,以及对应的getter和setter方法。 ``` public class Diary { private int id; private int user_id; private String title; private String content; private Date create_time; public Diary() {} public int getId() { return id; } public void setId(int id) { this.id = id; } public int getUserId() { return user_id; } public void setUserId(int user_id) { this.user_id = user_id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getCreateTime() { return create_time; } public void setCreateTime(Date create_time) { this.create_time = create_time; } } ``` 3. 创建DAO类 在src目录下创建一个名为DiaryDAO.java的DAO类,该类用于操作user_diary表,包括添加日记、删除日记、查询日记等方法。 ``` public class DiaryDAO { private Connection conn; public DiaryDAO() { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/diary?useUnicode=true&characterEncoding=utf-8", "root", "password"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } public void addDiary(Diary diary) throws SQLException { String sql = "INSERT INTO user_diary(user_id, title, content, create_time) VALUES (?, ?, ?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, diary.getUserId()); pstmt.setString(2, diary.getTitle()); pstmt.setString(3, diary.getContent()); pstmt.setTimestamp(4, new Timestamp(diary.getCreateTime().getTime())); pstmt.executeUpdate(); } public void deleteDiary(int id) throws SQLException { String sql = "DELETE FROM user_diary WHERE id=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); pstmt.executeUpdate(); } public List<Diary> getDiaryList(int userId) throws SQLException { String sql = "SELECT * FROM user_diary WHERE user_id=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, userId); ResultSet rs = pstmt.executeQuery(); List<Diary> diaryList = new ArrayList<>(); while (rs.next()) { Diary diary = new Diary(); diary.setId(rs.getInt("id")); diary.setUserId(rs.getInt("user_id")); diary.setTitle(rs.getString("title")); diary.setContent(rs.getString("content")); diary.setCreateTime(rs.getTimestamp("create_time")); diaryList.add(diary); } return diaryList; } } ``` 4. 创建JSP页面 在WebContent目录下创建一个名为index.jspJSP页面,该页面包含一个表单用于添加日记,以及一个表格用于显示用户的所有日记。 ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*,com.example.Diary,com.example.DiaryDAO"%> <% int userId = 1; // 假设当前用户的ID为1 String title = request.getParameter("title"); String content = request.getParameter("content"); if (title != null && content != null) { Diary diary = new Diary(); diary.setUserId(userId); diary.setTitle(title); diary.setContent(content); diary.setCreateTime(new Date()); DiaryDAO dao = new DiaryDAO(); dao.addDiary(diary); } DiaryDAO dao = new DiaryDAO(); List<Diary> diaryList = dao.getDiaryList(userId); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>日记管理系统</title> </head> <body> <h1>日记管理系统</h1> <h2>添加日记</h2> <form method="post" action="index.jsp"> <p>标题:<input type="text" name="title"></p> <p>内容:<textarea name="content"></textarea></p> <p><input type="submit" value="提交"></p> </form> <h2>我的日记</h2> <table border="1"> <tr> <th>ID</th> <th>标题</th> <th>内容</th> <th>创建时间</th> <th>操作</th> </tr> <% for (Diary diary : diaryList) { %> <tr> <td><%= diary.getId() %></td> <td><%= diary.getTitle() %></td> <td><%= diary.getContent() %></td> <td><%= diary.getCreateTime() %></td> <td><a href="delete.jsp?id=<%= diary.getId() %>">删除</a></td> </tr> <% } %> </table> </body> </html> ``` 5. 创建删除日记的JSP页面 在WebContent目录下创建一个名为delete.jspJSP页面,该页面用于删除指定ID的日记。 ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.example.DiaryDAO"%> <% int id = Integer.parseInt(request.getParameter("id")); DiaryDAO dao = new DiaryDAO(); dao.deleteDiary(id); response.sendRedirect("index.jsp"); %> ``` 以上就是一个简单的JSP+JavaBean大学日记管理系统的代码模,希望能对你有所帮助。需要注意的是,该模仅供参考,实际应用中还需要根据具体需求进行修改和完善。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值