能不能从smartUpload中,得到输出流?

小时候的天空 2003-04-03 05:23:58
因为我看smartUpload中的sample4必须要先在数据库中有这一条记录才行,我想直接插入一条,不知道行不行?
...全文
32 点赞 收藏 14
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ok 问题解决了,d80(今天没事做)的办法行的同!!!
散分
回复
xiaofenguser 2003-04-04
如果服务器操作权限不够,这里会出问题.
myFile.saveAs(fileName,myUpload.SAVE_PHYSICAL);
回复
谢谢大家帮忙,我试了一下d80(今天没事做) 的方法
代码如下:
<%@ page import="java.sql.*" %>
<%@page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="java.lang.*"%>
<%@ page language="java" import="com.jspsmart.upload.*"%>
<jsp:useBean id="myUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />

<HTML>
<BODY BGCOLOR="white">
<HR>
<% myUpload.initialize(pageContext);
myUpload.upload();
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:easy_people";
Connection con=null;
try{
Class.forName(sDBDriver);
con=DriverManager.getConnection(sConnStr,"webwth","wthpass1234");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("数据库连接出错: ");
System.err.println(e.getMessage());
}
String picname=myUpload.getRequest().getParameter("picname");
String sqlin = "insert into picture(name,image) values (?,?)";
com.jspsmart.upload.File myFile = myUpload.getFiles().getFile(0);
String fileName=myFile.getFileName();
if(!myFile.isMissing()){
//save data
myFile.saveAs(fileName,myUpload.SAVE_PHYSICAL);
System.out.println("============fileName="+fileName+"====");
java.io.File file=new java.io.File(fileName);
java.io.InputStream inStream=new java.io.FileInputStream(file);
int fileSize=myFile.getSize();
byte[] bytes = new byte[fileSize];
System.out.println("============fileSize="+fileSize+"====");
}

PreparedStatement ps = con.prepareStatement(sqlin);
ps.setString(1,picname);
ps.setBinaryStream(2,inStream,myUpload.getFiles().getFile(0).getSize());
ps.executeUpdate();
ps.close();
out.print("<script language=javascript>alert('图片保存成功');history.back();document.refresh;</script>");
%>
</BODY>
</HTML>
大家看一下有什么问题
回复
xiaofenguser 2003-04-04
反编译一下,具体的参考SmartUpload中的upload方法和File的saveAs.
最好是保持原来的不变的基础上,把upload方法改一下保存成自己的方法就可以.
回复
to: d80(今天没事做)
我先试一下
回复
d80 2003-04-04
那没有放到服务器的硬盘上。不会浪费服务器的空间的。
回复
我下载的是.class文件,是不是要反编译一下才行
回复
xiaofenguser 2003-04-04
修改smartUpload源码
回复
而且在服务器上放东西是要权限的吧??这个代码该怎么写呢
回复
to: d80(今天没事做)
你的代码我看了,你的思路是先把文件保存到服务器上,然后再入库,那么这样是不是浪费服务期的空间啊!!我想的是直接用程序得到数据流,另外我不知道java.io.File和
com.jspsmart.upload.File的区别在那里???
回复
d80 2003-04-04
看这个吧!!!

import java.sql.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jspsmart.upload.*;
/**
* <p>Title: </p>
* <p>Description: 上\u4F20文件到\u6570据中。形式在form中加上 ENCTYPE="multipart/form-data" </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class UpLoad extends HttpServlet{
public UpLoad() {
}
private ServletConfig config;
/**
* Init the servlet
*/
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}

final public ServletConfig getServletConfig() {
return config;
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("============UpLoad begin ddd=============");
String id=null;
SmartUpload mySmartUpload = new SmartUpload();
PrintWriter out = response.getWriter();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: "+e.getMessage());
}


try{
//Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@10.216.0.2:1521:ORCL","dms","dms");

conn.setAutoCommit(false);//\u8BBE置自\u52A8提交,以提高性能。
Statement stmt=conn.createStatement();
// Initialization
mySmartUpload.initialize(config, request, response);

mySmartUpload.setMaxFileSize(500 * 1024);

// Upload
mySmartUpload.upload();

//取得text框中的\u6570据
Enumeration enumer=mySmartUpload.getRequest().getParameterNames();
while(enumer.hasMoreElements()){
String key=(String) enumer.nextElement();
String[] values=mySmartUpload.getRequest().getParameterValues(key);
if(key.equals("id")){ //得到text框中的value值
id=values[0];
}
}

//取得文件和文件名
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
String fileName=myFile.getFileName();

if(!myFile.isMissing()){
//save data
myFile.saveAs(fileName,mySmartUpload.SAVE_PHYSICAL);
System.out.println("============fileName="+fileName+"====");
java.io.File file=new java.io.File(fileName);

java.io.InputStream inStream=new java.io.FileInputStream(file);

int fileSize=myFile.getSize();
byte[] bytes = new byte[fileSize];
System.out.println("============fileSize="+fileSize+"====");

//\u5C06\u6570据插入到\u6570据\u5E93中
String strSql="insert into zyw_test(id,name,content) values('"+id+"','"+fileName+"',empty_blob())";
stmt.execute(strSql);

ResultSet rs=stmt.executeQuery("select content from zyw_test where id='"+id+"' for update ");
if (rs.next()){
oracle.sql.BLOB blob = ((oracle.jdbc.OracleResultSet)rs).getBLOB("content");
OutputStream outStream = blob.getBinaryOutputStream();
inStream.read(bytes);
outStream.write(bytes);
outStream.flush();
stmt.execute("commit");
outStream.close();
}
inStream.close();
stmt.close();
out.println("upload sucess");
}else{
out.println("no file");
}
}catch(Exception ex){
out.println("upload fail");
ex.printStackTrace();
}
System.out.println("============UpLoad end=============");
}
}
回复
icecloud 2003-04-04
如需要代码短信我
回复
bjzhanghao 2003-04-03
我们公司以前做的方法是改一下upload的代码,能实现
回复
各位高手帮帮忙了!!!!!!!
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2003-04-03 05:23
社区公告
暂无公告