这里上传:
public class FileToDatabase {
public static void main(String[] args) throws Exception {
String fileName = "C:/input.txt";
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "root");
PreparedStatement pstmt = conn
.prepareStatement("insert into file( file, file_data) values ( ?, ?)");
pstmt.setString(1, file.getName());
pstmt.setBinaryStream(2, fis, (int) file.length());
pstmt.executeUpdate();
}
Run Code Online (Sandbox Code Playgroud)
在这里下载
我从应用程序服务器找到了一段很好的文件下载代码(在下面的链接中).如果您打算使用Web应用程序,可以在下载之前将文件从数据库缓存到应用程序中.(我对其他人对此替代方案的看法感兴趣)
链接:http://www.daniweb.com/software-development/java/threads/154128