文件上传文件保存到数据库是存在硬盘上,还是存在数据库中

文件上传怎么判断是否已经存在文件 - ThinkPHP框架
我在手册上看到有一个文件上传判断文件是否存在回调函数callback,却没有给出示例,我在数据库中记录了文件的md5值,请问如何写回调函数。
还有就是文件如果太大,是否后导致服务器压力增大,毕竟每次都要计算文件指纹。
ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。JavaEE中上传的文件是保存到数据库中好,还是保存到站点下面好?
文件可以存放在数据库。A方案是文件存入数据库。B方案是文件的链接存入数据库。
A优点:操作简单。备份与恢复数据库的时候,无需为文件保存在哪里而担心。&缺点:备份与恢复数据库的时候。。备份的数据库文件会非常的大。。读取时间会很长
B优点:数据库负担不重。。因为数据库里保存的只是文件的相对路径
&缺点:备份与恢复数据库的时候。。相对起来麻烦。。同时还要解决好。删除数据库的文件记录。是否还需要删除文件本身。。在文件读取的时候。还需要把文件的相对地址转换成绝对地址。
当然。。看系统的需要。采取的方式也是不一样的
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。android客户端把SD卡文件上传到服务器端并保存在PC硬盘文件夹中
在局域网内,实现从android客户端把手机SD卡上的文件上传到PC服务器端,并保存在PC硬盘的指定文件夹下。同时把PC端硬盘文件的目录和对文件的描述信息保存在中。
1、客户端关键代码:
(1)获得SD卡上的文件
* 获得文件路径和状态信息
private String getFiles() {
File path =
// 判断SD卡是否存在可用
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
path = Environment.getExternalStorageDirectory();
File[] files = path.listFiles();
for (File file : files) {
// 把路径如入集合中
if (file.getPath() != null
&& (file.getPath()
.substring(file.getPath().lastIndexOf("/") + 1)
.equals("DATA_RECORD.pcm"))) {
return file.getPath();
Toast.makeText(ASRMainActivity.this, "SD卡不可用!", 300).show();
(2)实现文件上传的方法
private void fileUpLoad() {
srcPath=getFiles();
new AsyncTask() {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "****************";
InputStreamReader isr =
FileInputS
DataOutputS
protected Void doInBackground(Void... params) {
String record_content=mSharedPreferences.getString("content","");
Log.i("测试",record_content);
// 首先指定服务器的路径URL
url = new URL(
"http://192.168.1.109:8080/MFCC/SpeechRecognizeAction?action_flag=upload&record_content="+record_content);
// 打开一个连接
con = (HttpURLConnection) url.openConnection();
// 设置一些属性
con.setDoInput(true);
con.setDoOutput(true);
con.setDefaultUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-boundary=" + boundary);
con.setReadTimeout(3000);
// 创建一个新的数据输出流,将数据写入指定基础输出流
dos = new DataOutputStream(con.getOutputStream());
// 将字符串按字节顺序 写出 到基础输出流中
// dos.writeBytes("Content-Disposition: form- name=\"uploads\";filename=1.txt");
// dos.writeBytes("Content-Disposition: form-");
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form- name=\"file\"; filename=\""
+ srcPath.substring(srcPath.lastIndexOf("/") + 1)
+ "\"" + end);
Log.i("tag",
"Content-Disposition: form- name=\"file\"; filename=\""
+ srcPath.substring(srcPath
.lastIndexOf("/") + 1) + "\"" + end);
dos.writeBytes(end);
// dos.writeBytes("1.txt");
// dos.writeBytes("Jonny-Resume.docx");
// 读取写到输出流中的数据
fis = new FileInputStream(srcPath);
byte[] buffer = new byte[8192]; // 8k
int count = 0;
count = fis.read(buffer);
Log.i("tag", count + "
********************");
while ((count = fis.read(buffer)) != -1) {
dos.write(buffer, 0, count);
Log.i("tag", "ok");
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
// 反馈给客户端的信息
InputStream is = con.getInputStream();
isr = new InputStreamReader(is, "utf-8");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
protected void onPostExecute(Void result) {
String result2 =
StringBuffer stringBuffer =
BufferedReader br =
if (isr == null) {
br = new BufferedReader(isr);
stringBuffer = new StringBuffer();
while ((result2 = br.readLine()) != null) {
stringBuffer.append(result2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (dos != null) {
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (fis != null) {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (stringBuffer != null)
Toast.makeText(ASRMainActivity.this,
new String(stringBuffer), Toast.LENGTH_LONG).show();
btn_uploadFile.setEnabled(true);
}.execute();
服务器端关键代码:
* 上传文件到PC,并把相关的文件信息写如数据库
* @param request
* @param response
private void uploadFile(HttpServletRequest request, HttpServletResponse response) {
String content = request.getParameter("record_content");
PrintWriter printWriter=
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();// 实例化一个文件工厂
// 构建一个文件上传类
ServletFileUpload servletFileUpload = new ServletFileUpload(
diskFileItemFactory);// 生成一个处理文件上传的servlet对象
servletFileUpload.setFileSizeMax(3 * 1024 * 1024);
servletFileUpload.setSizeMax(6 * 1024 * 1024);// 上传文件总大小
// 分析请求,并得到上传文件的FileItem对象
printWriter=response.getWriter();
List items = servletFileUpload.parseRequest(request);
Iterator e = items.iterator();
while (e.hasNext()) {
FileItem item = e.next();
if (item.getName() != null && !item.getName().equals("")) {
File file = new File("E://rawSpeechRecordData//");
File newFile =
if (!file.exists()) {
file.mkdir();
if (file.isDirectory()) {
SimpleDateFormat format = new SimpleDateFormat(
"yyyyMMddHHmmss");
String date = format.format(new Date(System
.currentTimeMillis()));
newFile = new File(
"E://rawSpeechRecordData//rawdata" + date
item.write(newFile);
//数据存入数据库
System.out.println("**********************"
+ newFile.getPath());
mFileInfoDao.addFilePathInfos(newFile.getPath(), content);
printWriter.write("数据提交成功!");
System.out.println(file);
System.out
.println("Content-Disposition: form- name=\"file\"; filename=\"");
System.out.println("**********************");
if (file.isDirectory()) {
SimpleDateFormat format = new SimpleDateFormat(
"yyyyMMddHHmmss");
String date = format.format(new Date(System
.currentTimeMillis()));
newFile = new File(
"E://rawSpeechRecordData//rawdata" + date
item.write(newFile);
//数据存入数据库
mFileInfoDao.addFilePathInfos(newFile.getPath(), content);
printWriter.write("数据提交成功!");
System.out.println("**********************"
+ newFile.getPath());
System.out.println(file);
System.out
.println("Content-Disposition: form- name=\"file\"; filename=\"");
System.out.println("**********************");
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
(1)数据库操作接口定义
public interface FileInfoDao {
* 添加文件路径到数据库
* @param filePath 文件路径
* @param content录音的详细信息
public void addFilePathInfos(String filePath,String content);
* 删除一条录音的路径信息
* @param content 录音的详细信息
public void deleteAFilePathInfo(String content);
* 删除所有的录音文件路径
public void deleteAllFilePathInfos();
* 查询所有的文件路径
public List<Map> getAllFilePaths();
(2)数据库操作实现类
public class FileInfoDaoimpl implements FileInfoDao {
// 表示定义数据库的用户名
private final String USERNAME = "root";
// 定义数据库的密码
private final String PASSWORD = "admin";
// 定义数据库的驱动信息
private final static String DRIVER = "com.mysql.jdbc.Driver";
// 定义数据库连接
private static Connection mC
// 定义访问数据库的地址
private final String URL = "jdbc:mysql://192.168.1.109:3306/fileinfos";
// 定义sql语句的执行对象
private PreparedS
// 定义查询返回的结果集合
private ResultSet resultS
public FileInfoDaoimpl() {
// 加载驱动
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
public void addFilePathInfos(String filePath, String content) {
PreparedStatement preparedStatement =
// 获得数据库连接
mConnection = DriverManager.getConnection(URL, "root", "admin");
// 获得SQL语句执行对象
preparedStatement = mConnection
.prepareStatement("insert into filepaths(file_path,record_content) values(?,?)");
// 设置参数
preparedStatement.setString(1, filePath);
preparedStatement.setString(2, content);
// 执行SQL语句
preparedStatement.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (preparedStatement != null) {
preparedStatement.close();
preparedStatement =
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (mConnection != null) {
mConnection.close();
mConnection =
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
public void deleteAFilePathInfo(String content) {
PreparedStatement preparedStatement=
//获得数据库连接
mConnection=DriverManager.getConnection(URL,"root","admin");
//获得SQL语句执行对象
preparedStatement=mConnection.prepareStatement("delete from filepaths where record_content=?");
//设置参数
preparedStatement.setString(1, content);
preparedStatement.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(preparedStatement!=null){
preparedStatement.close();
preparedStatement=
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(mConnection!=null){
mConnection.close();
mConnection=
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
public void deleteAllFilePathInfos() {
PreparedStatement preparedStatement=
mConnection=DriverManager.getConnection(URL, USERNAME,PASSWORD);
preparedStatement=mConnection.prepareStatement("delete from filepaths");
preparedStatement.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(preparedStatement!=null){
preparedStatement.close();
preparedStatement=
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(mConnection!=null){
mConnection.close();
mConnection=
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
public List<Map> getAllFilePaths() {
PreparedStatement preparedStatement=
List<Map> results=new ArrayList<Map>();
HashMap result=
mConnection=DriverManager.getConnection(URL, USERNAME, PASSWORD);
preparedStatement=mConnection.prepareStatement("select file_path,record_content from filepaths");
resultSet= preparedStatement.executeQuery();
while(resultSet.next()){
result=new HashMap();
result.put("filepath", resultSet.getString("file_path"));
result.put("record_content", resultSet.getString("record_content"));
results.add(result);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(preparedStatement!=null){
preparedStatement.close();
preparedStatement=
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(mConnection!=null){
mConnection.close();
mConnection=
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
(3)数据库操作对象的静态工厂方法(单例模式)
public class FilePathInfosDaoFactory {
private static FileInfoDao mFileInfoD
* 获得数据库操作单例对象
public static FileInfoDao getInstanse() {
if (mFileInfoDaoimpl == null) {
mFileInfoDaoimpl = new FileInfoDaoimpl();
return mFileInfoD
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'在网站中插入图片是保留在数据库还是目录?
没做过这方面的东西,突然好奇问问,在网站中插入图片是保留在数据库还是目录?我只知道用户名和密码,论坛发贴的文章是保留在数据库中,那么如果在论坛上传图片,是保留在哪里?如果是目录,是不是把论坛所有用户上传的图片都存储在一个目录??&还是存储在一个用户名文件夹/图片/ 中?&
去看看discuz怎么整的。。觉得应该不是数据库。。
你把图片存在文件系统,数据库里存放图片的路径,目录按照用户名分类便于维护呀
放数据库多麻烦,放到指定目录吧,然后把路径什么的存到数据库即可。
至于目录分类和名字这个问题,你想怎么搞都可以。
一般都是目录,当然也有通过编码存储到数据库的,号称为了提高前端显示速度,但是加重了数据库的负担

我要回帖

更多关于 文件上传数据库设计 的文章

 

随机推荐