用java如何一次性上传多张图片到数据库

用java如何一次性上传多张图片到数据库,第1张

你说的这个问题,其实是表的设计问题。

只要在t_b中,设计列为3列即可。

id

,

t_a_id

,

picture

添加多个图片,不过是sql、语句。

最简单的办法,是利用for()循环,如:

for(int

i

=

0i<图片数i++){

//你的sql *** 作语句如。

//insert

图片i

into

t_b..

}

分给偶吧。。

^

^!

我给你个简单的吧 这个可以实现三个一起上传 这个是我自己用的 做了点调整

uploadCore.php 页面代码

<?php

/*

* @(#)UploadFile.php (beta) 2005/2/19

*

* exBlog上传附件类,可同时处理用户多个上传文件。效验文件有效性后存储至指定目录。

* 可返回上传文件的相关有用信息供其它程序使用。(如文件名、类型、大小、保存路径)

* 使用方法请见本类底部(UploadFile类使用注释)信息。

*/

class UploadFile {

var $user_post_file = array()//用户上传的文件

var $user_name_u = array()//username

//var $user_name_y = array()//usertype

var $save_file_path //存放用户上传文件的路径

var $max_file_size//文件最大尺寸

var $last_error//记录最后一次出错信息

//默认允许用户上传的文件类型

var $allow_type = array('gif', 'jpg', 'png', 'zip', 'rar', 'txt', 'doc', 'pdf')

var $final_file_path//最终保存的文件名

var $save_info = array()//返回一组有用信息,用于提示用户。

/**

* 构造函数,用与初始化相关信息,用户待上传文件、存储路径等

*

* @param Array $file 用户上传的文件

* @param String $path 存储用户上传文件的路径

* @param Integer $size 允许用户上传文件的大小(字节)

* @param Array $type 此数组中存放允计用户上传的文件类型

*/

function UploadFile($file, $uname, $path, $size = 2097152, $type = '') {

$this->user_name_u = $uname

//$this->user_name_y = $utype

$this->user_post_file = $file

$this->save_file_path = $path

$this->max_file_size = $size//如果用户不填写文件大小,则默认为2M.

if ($type != '')

$this->allow_type = $type

}

/**

* 存储用户上传文件,检验合法性通过后,存储至指定位置。

* @access public

* @return int值为0时上传失败,非0表示上传成功的个数。

*/

function upload() {

for ($i = 0$i <count($this->user_post_file['name'])$i++) {

//如果当前文件上传功能,则执行下一步。

if ($this->user_post_file['error'][$i] == 0) {

//取当前文件名、临时文件名、大小、扩展名,后面将用到。

$userty = $this->user_name_y[$i]

$uuname = $this->user_name_u[$i]

$name = $this->user_post_file['name'][$i]

$tmpname = $this->user_post_file['tmp_name'][$i]

$size = $this->user_post_file['size'][$i]

$mime_type = $this->user_post_file['type'][$i]

$type = $this->getFileExt($this->user_post_file['name'][$i])

//检测当前上传文件大小是否合法。

if (!$this->checkSize($size)) {

$this->last_error = "这个文件的大小太大了. 您上传的文件名: ".$name

$this->halt($this->last_error)

continue

}

//检测当前上传文件扩展名是否合法。

if (!$this->checkuplod($type)) {

$this->last_error = "允许上传文件类型: .".$type." 您上传的文件名: ".$name

$this->halt($this->last_error)

continue

}

//检测当前上传文件是否非法提交。

if(!is_uploaded_file($tmpname)) {

$this->last_error = "文件非法提交. 您上传的文件名: ".$name

$this->halt($this->last_error)

continue

}

//移动文件后,重命名文件用。

$basename = $this->getBaseName($name, ".".$type)

//移动后的文件名

$saveas = $basename."-".time().".".$type

//$saveas = $basename.".".$type

//组合新文件名再存到指定目录下,格式:存储路径 + 文件名 + 时间 + 扩展名

$this->final_file_path = $this->save_file_path."/".$saveas

if(!move_uploaded_file($tmpname, $this->final_file_path)) {

$this->last_error = $this->user_post_file['error'][$i]

$this->halt($this->last_error)

continue

}

//存储当前文件的有关信息,以便其它程序调用。

$this->save_info[] = array("uname" =>$uuname,"name" =>$name, "type" =>$type,

"mime_type" =>$mime_type,

"size" =>$size, "saveas" =>$saveas,

"path" =>$this->final_file_path)

}

}

return count($this->save_info)//返回上传成功的文件数目

}

/**

* 返回一些有用的信息,以便用于其它地方。

* @access public

* @return Array 返回最终保存的路径

*/

function getSaveInfo() {

return $this->save_info

}

/**

* 检测用户提交文件大小是否合法

* @param Integer $size 用户上传文件的大小

* @access private

* @return boolean 如果为true说明大小合法,反之不合法

*/

function checkSize($size) {

if ($size >$this->max_file_size) {

return false

}

else {

return true

}

}

/**

* 检测用户提交文件类型是否合法

* @access private

* @return boolean 如果为true说明类型合法,反之不合法

*/

function checkType($extension) {

foreach ($this->allow_type as $type) {

//echo "<pre>"

//print_r($type)

//print_r($extension)

$str=substr($extension['name'][0],strrpos($extension['name'][0],'.')+1)

//echo $str

//exit()

if (strcasecmp($str, $type) == 0)

return true

}

return false

}

function checkuplod($extension) {

foreach ($this->allow_type as $type) {

//echo "<pre>"

//print_r($type)

//print_r($extension)

//$str=substr($extension['name'][0],strrpos($extension['name'][0],'.')+1)

//echo $str

//exit()

if (strcasecmp($extension, $type) == 0)

return true

}

return false

}

/**

* 显示出错信息

* @param $msg要显示的出错信息

* @access private

*/

function halt($msg) {

printf("<b><UploadFile Error:></b>%s <br>\n", $msg)

exit()

}

/**

* 取文件扩展名

* @param String $filename 给定要取扩展名的文件

* @access private

* @return String 返回给定文件扩展名

*/

function getFileExt($filename) {

$stuff = pathinfo($filename)

return $stuff['extension']

}

/**

* 取给定文件文件名,不包括扩展名。

* eg: getBaseName("j:/hexuzhong.jpg")//返回 hexuzhong

*

* @param String $filename 给定要取文件名的文件

* @access private

* @return String 返回文件名

*/

function getBaseName($filename, $type) {

$basename = basename($filename, $type)

return $basename

}

}

?>

调用和使用方法

$tmp=$Form

require ('uploadCore.php')

//设置允许用户上传的文件类型。

$type = array('gif', 'jpg', 'png', 'zip', 'rar', 'txt')

//实例化上传类,第一个参数为用户上传的文件组、第二个参数为存储路径、

//第三个参数为文件最大大小。如果不填则默认为2M

//第四个参数为充许用户上传的类型数组。如果不填则默认为gif, jpg, png, zip, rar, txt, doc, pdf

$upload = new UploadFile($user_upload_file,$user_name, $pathy, 500000, $type)

//print"<pre>"

//print_r($upload)

//上传用户文件,返回int值,为上传成功的文件个数。

$num = $upload->upload()

if ($num != 0) {

//echo "上传成功<br>"

$jeff_upload_info=$upload->getSaveInfo()

//取得文件的有关信息,文件名、类型、大小、路径。用print_r()打印出来。

//print_r($jeff_upload_info['uname'])

//print"<pre>"

//print_r($jeff_upload_info)

//exit()

//格式为: Array

// (

//[0] =>Array(

//[name] =>example.txt

//[type] =>txt

//[size] =>526

//[path] =>j:/tmp/example-1108898806.txt

//)

// )

//获得文件保存路径或者其他的信息

for ($jeff_upload_success_num = 0$jeff_upload_success_num <$num$jeff_upload_success_num++)

{

$tmp['zz_upload_file'] = $jeff_upload_info[$jeff_upload_success_num]['uname']

$tmp['zz_user_name'] = $jeff_upload_info[$jeff_upload_success_num]['name']

$tmp['zz_user_uname'] = $jeff_upload_info[$jeff_upload_success_num]['saveas']

$tmp['zz_size'] = $jeff_upload_info[$jeff_upload_success_num]['size']

$tmp['zz_addtime'] = date("Y-m-d")

$tmp['zz_passtime'] = date("Y-m-d")

//print"<pre>"

//print_r($tmp)

$q_sql = data_insert($tmp,'hczb_zzwd')//插入数据库类

//print_r($q_sql)

//$jeff_upload_success_url = "\r\n".'[img]'.$jeff_upload_info[$jeff_upload_success_num]['path'].'[/img]'

//$jeff_upload_success_img .= $jeff_upload_success_url//获得[img]代码

}

if($q_sql)

{

echo "<script>window.location.href='third.php'</script>"

$_SESSION['f'] = 3

}

else

{

echo "<script>alert('sorry, *** 作失败')window.location.href='third.php'</script>"

}

//echo $num."个文件上传成功"

}

else {

echo "<script>alert('sorry,上传失败,允许上传的格式是:'".$type."'')window.location.href='third.php'</script>"

}

表单调用:

<table width="91%" border="0" align="center" cellpadding="0" cellspacing="0" style="margin-top:12px">

<tr>

<td valign="top" ><fieldset>

<legend><img src="images/third_11.jpg" /></legend>

<table width="98%" border="0" cellspacing="0" cellpadding="0" style="margin-top:12px" align="center">

<tr>

<td class="tianjie"><table width="100%" border="0" cellspacing="0" cellpadding="0" >

<tr>

<td width="16%" align="center">附件序号 </td>

<td width="34%" align="center">附件名称 </td>

<td width="43%" align="center">附件说明</td>

<td width="7%" align="center"></td>

</tr>

</table>

<table width="100%" border="0" cellspacing="0" cellpadding="0" >

<tr>

<td width="16%" align="center">附件一 </td>

<td width="38%" align="center"><input name="user_upload_file[]" type="file" class="bg" id="filename" size="16" /></td>

<td width="43%" align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>

<td width="10" align="right"><img src="images/input_l.gif" width="6" height="28" /></td>

<td width="120">

<input name="user_name[]" type="text" id="user_name" class="srk" size="35" onchange="clear_errors(document.all.dis_error_user_name,this)"/></td>

<td width="9" align="left"><img src="images/input_r.gif" width="5" height="28" /></td>

</tr>

</table></td>

<td width="3%" align="center"></td>

</tr>

</table>

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="lx">

<tr>

<td width="16%" align="center">附件二 </td>

<td width="38%" align="center"><input name="user_upload_file[]" type="file" class="bg" id="filename" size="16" /></td>

<td width="43%" align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>

<td width="10" align="right"><img src="images/input_l.gif" width="6" height="28" /></td>

<td width="120">

<input name="user_name[]" type="text" id="user_name1" class="srk" size="35" onchange="clear_errors(document.all.dis_error_user_nameo,this)"/></td>

<td width="9" align="left"><img src="images/input_r.gif" width="5" height="28" /></td>

</tr>

</table></td>

<td width="3%" align="center"></td>

</tr>

</table>

<table width="100%" border="0" cellspacing="0" cellpadding="0" >

<tr>

<td width="16%" align="center">附件三 </td>

<td width="38%" align="center"><input name="user_upload_file[]" type="file" class="bg" id="filename" size="16" /></td>

<td width="43%" align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>

<td width="10" align="right"><img src="images/input_l.gif" width="6" height="28" /></td>

<td width="120">

<input name="user_name[]" type="text" id="user_name2" class="srk" size="35" onchange="clear_errors(document.all.dis_error_user_namet,this)"/></td>

<td width="9" align="left"><img src="images/input_r.gif" width="5" height="28" /></td>

</tr>

</table></td>

<td width="3%" align="center"></td>

</tr>

</table>

<table width="100%" border="0" cellspacing="0" cellpadding="0" style="margin:15px">

<tr>

<td align="center"><input type="submit" name="sub" value=" " style="border:0pxwidth:166pxheight:28pxbackground:url(images/third_19.jpg) no-repeatcursor:hand" /></td>

</tr>

</table>

</td>

</tr>

</table>

</fieldset></td>

</tr>

</table>

有哪里不明白的 M我 告诉你

我给你一个小例子,你自己看看吧,可能会由于我们数据库软件不同,可能需要修改一下。(我用的是 SQL Server)

下面是我自己做的一个类,实例化后即可使用。

using System

using System.Collections.Generic

using System.ComponentModel

using System.Data

using System.Data.OleDb

using System.Drawing

using System.Text

using System.Windows.Forms

using System.Data.SqlClient

using System.IO

namespace SQL_Query.MyClass

{

class ConnectionSQLClass

{

private string _server = string.Empty

private string _database = string.Empty

private string _uid = string.Empty

private string _pwd = string.Empty

private string _sqlConnection = string.Empty

public ConnectionSQLClass(string server, string database, string uid, string pwd)

{

_server = server

_database = database

_uid = uid

_pwd = pwd

_sqlConnection = "server=" + _server + "database=" + _database + "uid=" + _uid + "pwd=" + _pwd

}

//插入图片(table 表名、fieldName 存储图片字段名、imagePath 图片完整路径)

public bool Insert_Image(string table, string fieldName, string imagePath)

{

try

{

FileStream fs = new FileStream(imagePath, FileMode.Open)

byte[] imagebytes = new byte[fs.Length]

BinaryReader br = new BinaryReader(fs)

imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length))

//打开数所

SqlConnection con = new SqlConnection(_sqlConnection)

con.Open()

SqlCommand com = new SqlCommand("insert into " + table + "(" + fieldName + ")" + " values(@ImageList)", con)

com.Parameters.Add("ImageList", SqlDbType.Image)

com.Parameters["ImageList"].Value = imagebytes

com.ExecuteNonQuery()

con.Close()

}

catch { return false}

return true

}

//读取 fieldName 该字段中符合条件的所有图片(sql SQL查询语句、fieldName 存储图片字段名)

public List<Image>Get_Image(string sql, string fieldName)

{

List<Image>InformatoinCollection = new List<Image>()

SqlConnection cn = new SqlConnection(_sqlConnection)

try

{

cn.Open()

SqlCommand cm = new SqlCommand(sql, cn)

SqlDataReader dr = cm.ExecuteReader()

//MessageBox.Show(dr.HasRows.ToString())

if (!dr.HasRows)

{

return null

}

while (dr.Read())

{

MemoryStream ms1 = new MemoryStream((byte[])dr[fieldName])

Image image = Image.FromStream(ms1, true)

InformatoinCollection.Add(image)

}

dr.Close()

cn.Close()

}

catch

{

InformatoinCollection = null

}

return InformatoinCollection

}

}

}

希望对你有帮助!


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10716486.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-10
下一篇 2023-05-10

发表评论

登录后才能评论

评论列表(0条)

保存