一般文件上传并不会放在数据库上,我这是为了测试一下咯
if ("post".equalsIgnoreCase(request.getMethod())) { DiskFileUpload diskFileUpload = new DiskFileUpload();//文件上传的类 diskFileUpload.setHeaderEncoding("UTF-8");//设置utf-8 List<FileItem> list = diskFileUpload.parseRequest(request);//解析request for (FileItem fileItem : list) { if (!fileItem.isFormField()) { String filename = fileItem.getName().replace("\", "/");// filename = filename .substring(filename.lastIndexOf("/") + 1);//拼接名字 Connection con = null; PreparedStatement preparedStatement = null; try { String sql = "insert into blobTest values(?,?,?)"; con = DbManager.getConnection(); preparedStatement = con.prepareStatement(sql); preparedStatement.setString(1, filename);//第一个参数和第二个参数放名字和文件类型 preparedStatement.setString(2, fileItem.getContentType()); preparedStatement.setBinaryStream(3, fileItem.getInputStream(),//fileItem直接打开流输入进去 (int) fileItem.getSize()); preparedStatement.executeUpdate(); }
下载类
in1 = resultSet.getBinaryStream("file");//从数据库获得字段名为file的文件
out1 = response.getOutputStream();