能不能从smartUpload中,得到输出流? 我们公司以前做的方法是改一下upload的代码,能实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看这个吧!!!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============="); }} to: d80(今天没事做) 你的代码我看了,你的思路是先把文件保存到服务器上,然后再入库,那么这样是不是浪费服务期的空间啊!!我想的是直接用程序得到数据流,另外我不知道java.io.File和com.jspsmart.upload.File的区别在那里??? 我下载的是.class文件,是不是要反编译一下才行 to: d80(今天没事做) 我先试一下 反编译一下,具体的参考SmartUpload中的upload方法和File的saveAs.最好是保持原来的不变的基础上,把upload方法改一下保存成自己的方法就可以. 谢谢大家帮忙,我试了一下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>大家看一下有什么问题 如果服务器操作权限不够,这里会出问题.myFile.saveAs(fileName,myUpload.SAVE_PHYSICAL); ok 问题解决了,d80(今天没事做)的办法行的同!!!散分 使用<jsp:forward>如何跳转Action struts2 时间格式化问题 JAVA新手 基礎配置問題.幫幫忙. 如何实现表的刷新....... jsp 不能运行 好急呀! Highcharts动态设置series 标签中如何使用变量 关于使用weblogic的问题 jsp中如何实现根据数据库构建树型目录 文革问提 这样的功能能否实现?
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=============");
}
}
你的代码我看了,你的思路是先把文件保存到服务器上,然后再入库,那么这样是不是浪费服务期的空间啊!!我想的是直接用程序得到数据流,另外我不知道java.io.File和
com.jspsmart.upload.File的区别在那里???
我先试一下
最好是保持原来的不变的基础上,把upload方法改一下保存成自己的方法就可以.
代码如下:
<%@ 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>
大家看一下有什么问题
myFile.saveAs(fileName,myUpload.SAVE_PHYSICAL);
散分