第一天:
完成的工作:
1.文件的读写及存储在数据库中;
遇到的问题:
1.文件存储到SQL数据库的时候有乱码。
2.文件从数据库获取的地址有时候打不开;
第二天:
完成的工作:
1.测试模块的编写;
第三天:
完成的工作:
1.测试模块的编写;
第四天:
完成的工作:
1.利用Java对文件进行读写及存储;
package upload; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.sql.*; import java.text.SimpleDateFormat; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; public class UpLoad extends HttpServlet { /** * Constructor of the object. */ public UpLoad() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.getWriter().println("请以POST方式上传文件"); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ @SuppressWarnings({ "unchecked", "deprecation" }) public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { File file1 = null; String description1 = null; String XM_ID = null; String RW_ID = null; String GZ_ID = null; String C_ID=null; response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8");//设置传输编码 try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // com.microsoft.jdbc.sqlserver.SQLserveDriver String url = "jdbc:sqlserver://localhost:1433; DatabaseName=Bysj01"; // jdbc:microsoft:sqlserver://127.0.0.1:1433; // jdbc:microsoft.sqlserver://127.0.0.1:1433 try { Connection con = DriverManager.getConnection(url, "sa", "123"); long l = System.currentTimeMillis(); java.sql.Date date = new java.sql.Date(l); SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:MM:SS"); String id = dateFormat.format(date); int x = 0; for (int w = 0; w < 4; w++) { x = (int) (Math.random() * 9); id += x; } response.setContentType("text/html"); PrintWriter out = response.getWriter(); DiskFileUpload diskFileUpload = new DiskFileUpload(); try { List<FileItem> list = diskFileUpload.parseRequest(request); for (FileItem fileItem : list) { if (fileItem.isFormField()) { if ("description1".equals(fileItem.getFieldName())) description1 = new String(fileItem.getString() .getBytes(), "UTF-8"); } if ("XM_ID".equals(fileItem.getFieldName())) { XM_ID = new String(fileItem.getString() .getBytes(), "UTF-8"); } if ("RW_ID".equals(fileItem.getFieldName())) { RW_ID = new String(fileItem.getString() .getBytes(), "UTF-8"); } if ("GZ_ID".equals(fileItem.getFieldName())) { GZ_ID = new String(fileItem.getString() .getBytes(), "UTF-8"); } if ("C_ID".equals(fileItem.getFieldName())) { C_ID = new String(fileItem.getString() .getBytes(), "UTF-8"); } } else { if ("file1".equals(fileItem.getFieldName())) { File remoteFile = new File(new String(fileItem .getName().getBytes(), "UTF-8")); file1 = new File(this.getServletContext() .getRealPath("attachment"), remoteFile.getName()); file1.getParentFile().mkdirs(); file1.createNewFile(); InputStream ins = fileItem.getInputStream(); OutputStream ous = new FileOutputStream(file1); try { byte[] buffer = new byte[1024]; int len = 0; while ((len = ins.read(buffer)) > -1) ous.write(buffer, 0, len); } finally { ous.close(); ins.close(); } } } } } catch (FileUploadException e) { } if (file1 != null) { out.println("<div>"); out.println(" <div align='left'>file1:</div>"); out.println(" <div align='left'><a href='" + request.getContextPath() + "/attachment/" + file1.getName() + "'target=_blank>" + file1.getName() + "</a>"); out.println("</div>"); out.println("</div>"); // 插入数据库 String sql = "insert into WJ values (?,?,?,?,?,?,?,?,?)"; System.out.println(sql); PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, id);// 赋值id ps.setString(2, XM_ID);// 赋值id ps.setString(3, RW_ID);// 赋值文件说明 ps.setString(4, GZ_ID);// 赋值id ps.setString(5,file1.getAbsolutePath());// 赋值文件存储位置 ps.setString(6,file1.getName());// 赋值文件名称 ps.setString(7,description1);// 赋值文件说明 ps.setString(8, id);// 上传时间 ps.setString(9, C_ID);// 上传人 int i = ps.executeUpdate(); //执行插入数据操作,返回影响的行数 if (i == 1) { out.println("<script language='javaScript'> alert('发布成功');</script>"); response.setHeader("refresh", "1;url=../home/index.jsp"); } } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here } }