远程图片提交到数据库image字段的问题!

huhong81312 2003-11-12 05:17:50
我现在有数据库1个中有表1个内有字段iamge类型,现在我要通过JSP选择1个图片然后直接传送到数据库表中该字段。注意我的服务器在江苏而客户端(IE)可能在浙江或者广东,请问高手我该怎么做?最好给出些事例代码!谢谢!
...全文
49 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
LuZhou 2003-11-16
  • 打赏
  • 举报
回复
建议用流的方式,java的流原理就是为了便于网络上传输而特别设计的。
jordan119 2003-11-15
  • 打赏
  • 举报
回复
给你一个简单的办法

1.通过request对象取得上传文件输入流inputstream
2.服务器通过和数据库建立联系取得到数据库的一个输出流
这样就实现了你要求的功能
huhong81312 2003-11-13
  • 打赏
  • 举报
回复
都不对!
wenming168 2003-11-13
  • 打赏
  • 举报
回复
这是读出的类
package Servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class getImage extends HttpServlet
{

private String CONTENT_TYPE = "image/gif";
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
int ID = 0;
ServletOutputStream outt = response.getOutputStream();
try
{
ID = Integer.parseInt(request.getParameter("id"));
//System.out.println(ID);
}catch (Exception e){
response.sendRedirect("../ErroePage.jsp");
return;
}

try
{
byte[] buff = this.getCover(ID);
response.setContentType(CONTENT_TYPE);
outt.flush();
outt.write(buff);
outt.flush();
}catch (IOException ioe){
ioe.printStackTrace();
}
}

private synchronized byte[] getCover(int ID1)
{
Context ctx=null;
Connection Conn = null;
Statement stmt = null;
ResultSet rs = null;
byte[] buf = null;
try{
ctx = new InitialContext();
//DataSource ds = (DataSource)ctx.lookup("OracleDS");
DataSource ds = (DataSource)ctx.lookup("SqlServerDs");
Conn = ds.getConnection();
String searchSql="SELECT MIME,NR FROM TEST WHERE ID="+ ID1;
stmt = Conn.createStatement();
rs = stmt.executeQuery(searchSql);
rs.next() ;
CONTENT_TYPE = rs.getString("MIME");
buf = rs.getBytes("NR");
//System.out.println(CONTENT_TYPE);
}catch(Exception sqle){
System.err.println("Error in CoverServlet : getCover()-" + sqle);
sqle.printStackTrace() ;
}finally{
try
{
stmt.close() ;
Conn.close() ;
}catch (Exception e){
e.printStackTrace();
}
}
return buf;
}

public void destroy()
{
}
}

jsp文件
<p><a href="aaaa.doc">adfasdfasdf </a><img src="../servlet/getImage?id=1" width="103" height="34">
<a href="../servlet/getImage?id=2">SFADFASD</a> </p>
<p> </p>
wenming168 2003-11-13
  • 打赏
  • 举报
回复
用smartupload可以。我做过写入和读出。
<%@page contentType="text/html; charset=gb2312" language="java"
import="javax.sql.*"
import="javax.naming.*"
import = "java.sql.*"
import = "java.io.*"
%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
String add=null,medianame=null,exname=null,name1=null,sql=null,address=null,TypeMIME=null,SubTypeMIME=null,FilePathName=null,msg1=null;
String wwmj=null,wwmj1=null,EXT1=null;
int size1=0;
Connection Conn=null;
ResultSet Rs=null;
Statement Stmt = null;
add=request.getParameter("add");
if (add==null)
{
add="0";
}
if (add.equals("1"))
{
mySmartUpload.initialize(pageContext);
mySmartUpload.setTotalMaxFileSize(6000000);
//mySmartUpload.setAllowedFilesList("txt,doc,xls,jpg,gif");

mySmartUpload.upload();
for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
{
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("OracleDS");
Conn = ds.getConnection();
medianame=myFile.getFileName();
exname=myFile.getFileExt();
size1=myFile.getSize();
TypeMIME=myFile.getTypeMIME();
SubTypeMIME=myFile.getSubTypeMIME();
FilePathName=myFile.getFilePathName();
Statement aStmt = Conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
Rs = aStmt.executeQuery("SELECT MC,NR,DX,MIME,EXT FROM TEST");
Rs.moveToInsertRow();
Rs.updateString("MC",medianame);
Rs.updateInt("DX",size1);
Rs.updateString("MIME",TypeMIME);
Rs.updateString("EXT",exname);
byte buf[] = new byte[myFile.getSize()];
for (int j=0;j<myFile.getSize();j++)
{
//out.println(myFile.getBinaryData(j));
buf[j]=myFile.getBinaryData(j);
}
//File file1 = new File(FilePathName);
//FileInputStream fis = new FileInputStream(file1);
//InputStream is = fis;
//Rs.updateBinaryStream("NR",is,new Long(file1.length()).intValue());

myFile.fileToField(Rs,"NR");
//ByteArrayInputStream bas = new ByteArrayInputStream(buf);

//Rs.updateBytes("NR",buf);
Rs.insertRow();
Rs.moveToCurrentRow();
Rs.close();
Conn.close();
out.println("FieldName = " + medianame + "<BR>");
out.println("Size = " + myFile.getSize() + "<BR>");
out.println("FileExt = " + myFile.getFileExt() + "<BR>");
out.println("FilePathName = " + myFile.getFilePathName() + "<BR>");
out.println("ContentType = " + myFile.getContentType() + "<BR>");
out.println("ContentDisp = " + myFile.getContentDisp() + "<BR>");
out.println("TypeMIME = " + myFile.getTypeMIME() + "<BR>");
out.println("SubTypeMIME = " + myFile.getSubTypeMIME() + "<BR>");
break;
}
}
}
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="../css/text.css" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="save.jsp?add=1" enctype="multipart/form-data">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td width="6%"><a href="aaaa.doc">asdasd</a> </td>
<td width="94%">
<input type="file" style="width:50%" name="file">
<input type="submit" name="Submit" value="上传">
</td>
</tr>
<tr>
<td width="6%"> </td>
<td width="94%"> </td>
</tr>
<tr>
<td width="6%"> </td>
<td width="94%">文件大小应小于<font color="#FF0000">6</font>兆 </td>
</tr>
</table>
</form>
</body>
</html>
这是一个插入代码,你整理一下试试吧
huhong81312 2003-11-12
  • 打赏
  • 举报
回复
注意我要求直接将图片的内容保存到数据库中的image字段中如果先保存到服务器上的1个临时文件中然后在提交到数据库中是不行的。因为如果1个用户在提交图片是没问题的,如果有3个4个甚至更多的用用户提交的话就会出现图片异常的!
jordan119 2003-11-12
  • 打赏
  • 举报
回复
提交页面
--------------------------------------------------------------
upload up=new upload();
up.setDictionary("\tomcat\webapps\dir\upload");
up.upfile(request);

注意我现在使用的是 tomcat 服务器
jordan119 2003-11-12
  • 打赏
  • 举报
回复
上传页面
-----------------------------------------------------
<% request.setCharacterEncoding("GB2312") ; %>
<center>File upload test</center>
<br>
<table><tr>
<form method="post" enctype="multipart/form-data" action="upload.jsp">
<td> <input type=file size=20 name="fname"> </td>
<td> <input type=Submit value=Upload> </td> </form>
</tr></table>
<!--enctype="multipart/form-data"-->
jordan119 2003-11-12
  • 打赏
  • 举报
回复
第一步上传图片到数据库

--------------------------------------------------package com.jh.zy;

import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletException;

public class upload{
private static String newline = "\n";
private String uploadDirectory = ".";
private String ContentType = "";
private String CharacterEncoding = "";

private String outerFile=""; //用于外部获取的文件名

private String getFileName(String s){
int i = s.lastIndexOf("\\");
if(i < 0 || i >= s.length() - 1){
i = s.lastIndexOf("/");
if(i < 0 || i >= s.length() - 1)
return s;
}
return s.substring(i + 1);
}

public String getName()
{
return outerFile;
}

public void setUploadDirectory(String s){
uploadDirectory = s;
}

public void setContentType(String s){
ContentType = s;
int j;
if((j = ContentType.indexOf("boundary=")) != -1){
ContentType = ContentType.substring(j + 9);
ContentType = "--" + ContentType;
}
}

public void setCharacterEncoding(String s){
CharacterEncoding = s;
}

public void uploadFile( HttpServletRequest req) throws ServletException, IOException{
setCharacterEncoding(req.getCharacterEncoding());
setContentType(req.getContentType());
uploadFile(req.getInputStream());
}

public void uploadFile( ServletInputStream servletinputstream) throws ServletException, IOException{

String s5 = null;
String filename = null;
byte Linebyte[] = new byte[4096];
byte outLinebyte[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];

String line;
//得到文件名
while((line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null){
int i = line.indexOf("filename=");
if(i >= 0){
line = line.substring(i + 10);
if((i = line.indexOf("\"")) > 0)
line = line.substring(0, i);
break;
}
}

filename = line;

System.out.println("上传文件:" + filename);

if(filename != null && !filename.equals("\"")){
filename = getFileName(filename);

System.out.println("不包含路径的文件名:" + filename);
outerFile=filename;

String sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);
if(sContentType.indexOf("Content-Type") >= 0)
readLine(Linebyte, ai, servletinputstream, CharacterEncoding);

//File(String parent, String child)
//Creates a new File instance from a parent pathname string
//and a child pathname string.

System.out.println("上传目录:" + uploadDirectory);

File file = new File(uploadDirectory, filename);

//FileOutputStream(File file)
//Creates a file output stream to write to the file represented
//by the specified File object.
FileOutputStream fileoutputstream = new FileOutputStream(file);

while((sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null){
if(sContentType.indexOf(ContentType) == 0 && Linebyte[0] == 45)
break;

if(s5 != null){
//write(byte[] b, int off, int len)
//Writes len bytes from the specified byte array starting
//at offset off to this file output stream.
fileoutputstream.write(outLinebyte, 0, ai1[0]);
fileoutputstream.flush();
}
s5 = readLine(outLinebyte, ai1, servletinputstream, CharacterEncoding);
if(s5 == null || s5.indexOf(ContentType) == 0 && outLinebyte[0] == 45)
break;
fileoutputstream.write(Linebyte, 0, ai[0]);
fileoutputstream.flush();
}

byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s5 != null && outLinebyte[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(outLinebyte, 0, ai1[0] - newline.length() * byte0);
if(sContentType != null && Linebyte[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(Linebyte, 0, ai[0] - newline.length() * byte0);

fileoutputstream.close();
}
}

private String readLine(byte Linebyte[], int ai[],
ServletInputStream servletinputstream,
String CharacterEncoding){
try{
//readLine(byte[] buffer, int offset, int length)
//Reads a line from the POST data.
ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
if(ai[0] == -1)
return null;
}catch(IOException _ex){
return null;
}
try{
if(CharacterEncoding == null){
//用缺省的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length)
return new String(Linebyte, 0, ai[0]);
}else{
//用给定的编码方式把给定的byte数组转换为字符串
//String(byte[] bytes, int offset, int length, String enc)
return new String(Linebyte, 0, ai[0], CharacterEncoding);
}
}catch(Exception _ex){
return null;
}
}
/*
public int readLine(byte[] buffer,
int offset,
int length) throws java.io.IOException
从POST来的数据中读一行
参数:
buffer - buffer to hold the line data
offset - offset into the buffer to start
length - maximum number of bytes to read.
Returns:
number of bytes read or -1 on the end of line.
*/
}
permanece 2003-11-12
  • 打赏
  • 举报
回复
关注着
ddbean 2003-11-12
  • 打赏
  • 举报
回复
关注

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧