使用IDEA学习JSP代码第016课

网友投稿 942 2022-11-16

使用IDEA学习jsP代码第016课

使用IDEA学习JSP代码第016课

new361.java

package pack03;import javax.servlet.RequestDispatcher;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.javax.servlet.javax.servlet.javax.servlet.java.io.*;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;@WebServlet(name = "new361",urlPatterns = "/page04/new361")public class new361 extends HttpServlet{ @Override public void init(ServletConfig config) throws ServletException { super.init(config); try { Class.forName("com.mysql.jdbc.Driver"); } catch (Exception ignored) { } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); new354 login = (new354)session.getAttribute("login"); boolean ok = true; if(login == null) { ok = false; response.sendRedirect("new355.jsp"); } if(ok == true) { String loginName = login.getLoginName(); uploadFileMethod(request,response,loginName); } } private void uploadFileMethod(HttpServletRequest request, HttpServletResponse response, String loginName) { new358 upFile = new new358(); String backNews = ""; try { HttpSession session = request.getSession(true); request.setAttribute("upFile",upFile); String tempFileName = (String)session.getId(); File f1 = new File(tempFileName); FileOutputStream out = new FileOutputStream(f1); InputStream in = request.getInputStream(); byte[] b = new byte[10000]; int n; while ((n = in.read(b)) != -1) { out.write(b,0,n); } out.close(); in.close(); RandomAccessFile random = new RandomAccessFile(f1,"r"); int second = 1; String secondLine = null; while (second <= 2) { secondLine = random.readLine(); second++; } int position = secondLine.lastIndexOf("\\"); String fileName = secondLine.substring(position+1,secondLine.length()-1); byte[] cc = fileName.getBytes("ISO-8859-1"); fileName = new String(cc); fileName = fileName.replace(" ",""); String checkedStr = fileName.substring(0,fileName.indexOf(".")); boolean isLetterOrDigit = true; for (int i=0;i='a') || (c<='Z' && c>='A') || (c<='9' && c>='0')) { isLetterOrDigit = false; break; } } if(isLetterOrDigit == false) { response.sendRedirect("new359.jsp"); } String saveFileName = loginName.concat(fileName); random.seek(0); long forthEndPosition = 0; int forth = 1; while ((n=random.readByte()) != -1 && (forth <= 4)) { if(n == '\n') { forthEndPosition = random.getFilePointer(); forth++; } } File dir = new File("D:/aaa"); dir.mkdir(); File[] files = dir.listFiles(); for (int k=0;k= 0) && (j<= 6)) { mark--; random.seek(mark); n = random.readByte(); if(n == '\n') { endPosition = random.getFilePointer(); j++; } } random.seek(forthEndPosition); long startPoint = random.getFilePointer(); while (startPoint < endPosition - 1) { n = random.readByte(); random2.write(n); startPoint = random.getFilePointer(); } random2.close(); random.close(); String url = "jdbc:mysql://localhost:3306/test?&useUnicode=true&characterEncoding=utf8"; Connection conn = DriverManager.getConnection(url,"root","mysql123"); String sql = "select * from ab_member where loginName=?"; PreparedStatement ptmt = conn.prepareStatement(sql); ptmt.setString(1,loginName); ResultSet rs = ptmt.executeQuery(); if (rs.next()) { if(isLetterOrDigit) { rs.updateString(8,saveFileName); int index = rs.getRow(); rs.absolute(index); rs.updateRow(); backNews = fileName +"成功上传"; upFile.setFileName(fileName); upFile.setSavedFileName(saveFileName); upFile.setBackNews(backNews); } } conn.close(); f1.delete(); } catch (Exception ex) { backNews = "" + ex; upFile.setBackNews(backNews); } try { RequestDispatcher dispatcher = request.getRequestDispatcher("new360.jsp"); dispatcher.forward(request,response); } catch (Exception ex) { } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }}

new362.java

package pack03;//memberInformpublic class new362{ private String loginName; private String sex; private String email; private String phone; private String message; private int age; private String backNews; public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getBackNews() { return backNews; } public void setBackNews(String backNews) { this.backNews = backNews; }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:mysql用in走索引吗?(mysql-8.0.21、5.5.40是不一样的)实践出真知——看完就要注意in的用法了
下一篇:四剑客与Code Review的恩怨情仇:“始乱终弃”到“浪子回头”
相关文章

 发表评论

暂时没有评论,来抢沙发吧~