mysql - 如何使用jsp servlet代码将文件路径和文件名存储到数据库(mysql)中?

标签 mysql jsp

我已经上传了文件夹到服务器文件夹,所以现在我想将文件路径和文件名存储到Mysql数据库中,并将数据库中存储的文件以树形结构的方式显示到前端。

这是代码请帮助

page.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" %>
<%@page import="java.io.*,java.net.*"%>
<HTML>
<FORM ENCTYPE="multipart/form-data" ACTION="uploadandstore.jsp" METHOD=POST>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<br><br><br>
<tr><td colspan="" align="center"> </td></tr>
<tr><td><b>Choose the file</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr><td colspan="2" align="center"> </td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Submit"> </td></tr>


<table>
</center>
</FORM>
</HTML>

uploadandstore.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.io.*, java.sql.*"%>
<%

String saveFile="";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
saveFile="C:/UploadedFiles/"+saveFile;
File f = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>
   
     <%
 
   }
%>
    <a href="viewFiles.jsp">View Files</a>

viewFiles.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.io.*"%>
<html>
<table>

<%
File f = new File("C:/UploadedFiles/");
        File[] files = f.listFiles();
        for(int i=0;i<files.length;i++){
            String name=files[i].getName();
            String path=files[i].getPath();
            
%>
<tr><td><%=name%></td><td><a href="download.jsp?f=<%=path%>">Download</a></td></tr>
     <%
        }
%>
</table>
</html>

download.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.io.*,java.net.*"%>

<%!
public static String getMimeType(String fileUrl)
    throws java.io.IOException, MalformedURLException 
  {
    String type = null;
    URL u = new URL(fileUrl);
    URLConnection uc = null;
    uc = u.openConnection();
    type = uc.getContentType();
    return type;
  }

%>
 <%
    String file=request.getParameter("f");
    File f = new File (file);
    String filename=f.getName();
    String type=getMimeType("file:"+file);

    response.setContentType (type);
    response.setHeader ("Content-Disposition", "attachment; filename=\""+filename+"\"");

    String name = f.getName().substring(f.getName().lastIndexOf("/") ,f.getName().length());
    InputStream in = new FileInputStream(f);
        ServletOutputStream outs = response.getOutputStream();

        int bit = 256;
        int i = 0;
            try {
                    while ((bit) >= 0) {
                        bit = in.read();
                        outs.write(bit);
                    }
                        } catch (IOException ioe) {
                        ioe.printStackTrace(System.out);
                    }
                        outs.flush();
                    outs.close();
                    in.close(); 

        %>

最佳答案

好的,根据评论

String filePath=f.getAbsolutePath(); // this will get the file path
try(Connection conn=getDBConnection();// here it will open a DB connection
PreparedStatement peparedState=conn.preparStatement("INSERT INTO PATHS SET path=?")// Query
peparedState.setString(1,filePath); // setting the variable corresponding to '?' 
preparedState.executeUpdate()); // executeQuery

关于mysql - 如何使用jsp servlet代码将文件路径和文件名存储到数据库(mysql)中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43387261/

相关文章:

http - 输入类型 "hidden"vs 文本区域

php - PDO 声明与工作 session ?

Css 和 js 未在 spring mvc 应用程序中加载

jsp - 在不使用 session 上下文的情况下在 Web 应用程序中保留值

php - 多语言站点 URL 和语言路径

图像未加载到部署在 webapps ROOT 文件夹内的 jsp 中

java - 需要将 servlet 输出获取到 html 文件中

MySQL:打印计数值

mysql - mysql 更新查询是否受益于索引?

c# - 使用 SSH.NET 库从 .NET 连接到 MySQL