bjcger 2006-3-10 15:47
使用smartupload上传后,下载出现问题。
我在使用smartupload时,文件能够写入sql-server中,但是却只是一部分,比如在下载上传后的文本文件时,文件内容不全,下载上传过的图片时,图片不能完全显示。是不是要更改数据库中字段的数据类型呢,我用的是varbinary类型,我怀疑是不是类的问题,请各位帮忙解决。谢谢。
jiyangsoft 2006-3-10 15:47
image字段类型
jiyanggame 2006-3-10 15:48
我做上传的时候,通常不把图片啊文件啊之类的上传到数据库中,而是上传到服务器上的某个特定的文件夹里,然后将文件的路径存入数据库中,使用的时候通过程序读路径就可以了。
梦随风飘 2006-3-10 15:49
现在我和你遇到的问题有些相关,还希望我们能相互讨论讨论。我和你的方法要求都一样,我也实现了成功的上传,但是当我从数据库中读数据写到文件中时,如果指定文件的路径是“D:\”+文件名时,它都会成功,而且数据完整,但是如果指定文件的路径是其他时,比如是程序下面的某个文件夹它就提示错误信息,“D:\tomcat\webapps\project\tmpfile\project1批示.txt (系统找不到指定的路径。)”我的代码如下:
String mainpath=application.getRealPath("");
System.out.println(mainpath);
ResultSet rs=executeQuery(sql2);
while(rs.next())
{
docu_id=rs.getString(2);
docu_code=rs.getString(3);
docu_inner_code=rs.getString(4);
docu_sort=rs.getString(5);
refer_date=rs.getString(9).substring(0,10);
ResultSet rs0=executeQuery("select * from uploads where id='"+docu_id+"'");
while(rs0.next())
{
docu_name=rs0.getString(2);
locate=mainpath+lowpath+docu_name;
//locate="D:\\"+docu_name;
System.out.println(locate);
byte[] content=rs0.getBytes(3);
FileOutputStream fos=new FileOutputStream(locate);
for(int i=0;i<content.length;i++)
{
fos.write(content[i]);
}
fos.close();
}
Backer456 2006-3-10 15:50
SAMLE4上传的代码
<%@ page language="java" import="java.sql.*,com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<HTML>
<BODY BGCOLOR="white">
<H1>jspSmartUpload : Sample 4</H1>
<HR>
<%
// Variables
int count=0;
// Connect to the database
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://server:1433;DatabaseName=test","sa","");
// SQL Requestconn=DriverManager.getConnection(sconnstr,"sa","");
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY ,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM TFILES WHERE ID=3");
if (rs.next()){
// Initialization
mySmartUpload.initialize(pageContext);
mySmartUpload.setMaxFileSize(500 * 1024);
// Upload
mySmartUpload.upload();
// upload file in the DB if this file is not missing
if (!mySmartUpload.getFiles().getFile(0).isMissing()){
try {
rs.updateString("FILENAME",mySmartUpload.getFiles().getFile(0).getFileName());
// Add the current file in the DB field
mySmartUpload.getFiles().getFile(0).fileToField(rs,"FILE");
// Update
rs.updateRow();
count++;
} catch(Exception e) {
out.println("An error occurs : " + e.toString());
}
}
}
// Display the number of files uploaded
out.println(count + " file(s) uploaded in the database.");
rs.close();
stmt.close();
con.close();
%>
</BODY></HTML>
下载的代码,原来的sample7,经过改装
<%@ page language="java" import="java.sql.*,com.jspsmart.upload.*"%><jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /><%
// Connect to the database
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://server:1433;DatabaseName=test","sa","");
// SQL Request
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM TFILES where id=3");
// if the resultset is not null
if (rs.next()){
String FileName=rs.getString("FileName");
// Initialization
mySmartUpload.initialize(pageContext);
mySmartUpload.setTotalMaxFileSize(200000);
// Download field
mySmartUpload.downloadField(rs,"FILE","application/x-msdownload", "2.txt");
// mySmartUpload.fieldToFile(rs,"FILE","c:\\temp\\sample7.txt");
}
rs.close();
stmt.close();
con.close();
%>