javazoom.upload.UploadFile;参考
http://www.javazoom.net/jzservlets/uploadbean/uploadbean.htmlv
http://www.javazoom.net/jzservlets/uploadbean/uploadbean.htmlv
解决方案 »
- 如何加入orkut
- 数据库的驱动程序 jdbc配置转玛的问题
- IE6下url被截断
- 跟大家探讨一个问题:
- jsp中有没有类似ASP的response.redirect "test.asp"重定向功能?
- 后学末进,请指教一二!!!
- java bean问题 调用不了包中的java类!!!!!!
- 一般在读写文件时都是对服务器上的文本文件进行读写,比如用文件形式做的留言簿。我现在急需实现读写客户端自己机器上的文本文件,怎么能
- 一个简单问题,作多层的循环的时候……
- springMVC整合Ibatis出错。。
- 请问一个问题,急!!!!
- j2se1.3难道没有java.servlet或java.servlet.http包吗?我找不到
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=============");
}
}
myFile.saveAs(fileName,mySmartUpload.SAVE_PHYSICAL);
你还是先将文件上传到服务器上,再写到oracle里啊!
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:test","test","test");
con.setAutoCommit(false);
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY ,ResultSet.CONCUR_UPDATABLE);
String sql ="SELECT picture,pictname FROM Tpicturen where pictureid='tes' for update";
ResultSet rs=stmt.executeQuery(sql);if (rs.next()){ // Initialization
mySmartUpload.initialize(pageContext); // Upload
mySmartUpload.upload(); // upload file in the DB if this file is not missing
if (!mySmartUpload.getFiles().getFile(0).isMissing()){ try { //rs.updateString("pictname",mySmartUpload.getFiles().getFile(0).getFileName());
// Add the current file in the DB field
mySmartUpload.getFiles().getFile(0).fileToField(rs,"picture"); // Update
rs.updateRow();
count++;
} catch(Exception e) {
out.println("An error occurs : " + e.toString());
} } }
out.println(count + " file(s) uploaded in the database.");
con.commit();
rs.close();
stmt.close();
con.close();
错误信息:java.sql.SQLException: 内部错误: Unable to construct a Datum from the specified
<html>
<head>upload</head>
<body>
<hr>
<form method="post" action="/UploadFileServlet" enctype="multipart/form-data">
<p>Îļþ:<input type = "file" name="FileData" size="20"></p>
<p><input type="submit" value="UpLoad" name="uploadfile"></p>
</form>
</body>
</html>
* <p>Title: UploadFileServlet</p>
* <p>Description: Upload a file to the serve</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: shadow</p>
* @author shadow
* @version 1.0 final
*/
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletException;
import java.sql.*;
import oracle.sql.*public class UploadFileServlet extends HttpServlet { private ServletInputStream sin = null;
private OutputStream fout = null;
private String CharacterEncoding = "";
private String ContentType = "";
private String filename = ""; private byte temp[] = new byte[4096]; protected static final String newline = "\n";
protected static final String UploadDir = "./config/mydomain/applications/DefaultWebApp"; protected final void doPost(HttpServletRequest request, HttpServletResponse response) {
System.out.println("************************************");
upload(request, response);
} protected final void doGet(HttpServletRequest request, HttpServletResponse response) {
System.out.println("************************************");
System.out.println("request.getMethod():" + request.getMethod());
// upload(request, response);
System.out.println(request.getMethod() + " CAN NOT UPLOAD A FILE");
} public void upload(HttpServletRequest request, HttpServletResponse response) { String ContentType = "";
int i = 0; setCharacterEncoding(request.getCharacterEncoding());
System.out.println("request.getMethod():" + request.getMethod());
System.out.println("request.getContentType():" + request.getContentType());
System.out.println("ok");
setContentType(request.getContentType()); try {
sin = request.getInputStream();
filename = getFileName(sin);
//**************
i = sin.readLine(temp, 0, temp.length);
if (this.CharacterEncoding != null) {
ContentType = new String(temp, 0, i, this.CharacterEncoding);
}
else {
ContentType = new String(temp, 0, i);
}
System.out.println("ContentType:" + ContentType);
if (ContentType.indexOf("Content-Type") >= 0) {
System.out.println("uploading ...");
sin.readLine(temp, 0, temp.length);
}
//**************
if ((filename != null) || (!filename.equals(""))) {
fout = new FileOutputStream(new File(UploadDir, filename));//write to file
try {//write to blob
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@133.166.200.73:1521:ora73";
String user="asset";
String password="asset";
Connection con= DriverManager.getConnection(url,user,password);
con.setAutoCommit(false); //*
String sql = "insert into sandwichtest values('00000013', empty_clob(), empty_blob())";
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
con.commit(); //*
sql = "select blobimage from sandwichtest where id='00000013' for update";
ResultSet rs = stmt.executeQuery(sql); oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("blobimage");
java.io.OutputStream outs = blob.getBinaryOutputStream(); while ((i = sin.readLine(temp, 0, temp.length)) != -1) { if (this.CharacterEncoding != null) {
ContentType = new String(temp, 0, i, this.CharacterEncoding);
}
else {
ContentType = new String(temp, 0, i);
}
if ((ContentType.indexOf(this.ContentType) == 0)&&(temp[0] == 45)) {
System.out.println("this.ContentType:" + this.ContentType);
break;
}
//write to file
fout.write(temp,0,i); outs.write(temp);
outs.flush();
// fout.write(newline.getBytes());
}
}
}
catch (IOException ioe) {
System.out.print(ioe.getMessage());
}
finally {
try {
fout.close();
outs.close();
}
catch (Exception e) {
}
}
System.out.println("***** FINISHED ******"); }
//request.getContentType()·½·¨µÃµ½String s
protected void setContentType(String s) {
ContentType = s;
int j;
System.out.println("ContentType.indexOf(\"boundary=\"):" + ContentType.indexOf("boundary="));
if((j = ContentType.indexOf("boundary=")) != -1) {
ContentType = ContentType.substring(j + 9);
ContentType = "--" + ContentType;
}
System.out.println("ContentType: " + ContentType);
//it is commonly null
}
//request.getCharacterEncoding()·½·¨µÃµ½String s
protected void setCharacterEncoding(String s) {
CharacterEncoding = s;
System.out.println("CharacterEncoding: " + s);
}
//get filename
private String getFileName(ServletInputStream sin) {
String filename = "";
String str = ""; int i = 0;
int j = 0;
try { while ((i = sin.readLine(temp, 0, temp.length)) != -1) {
if (this.CharacterEncoding != null) {
str = new String(temp, 0, i, this.CharacterEncoding);
}
else {
str = new String(temp, 0, i);
}
if ((j = str.indexOf("filename=")) != -1) {
j += 10;
str = str.substring(j);
System.out.println("str:" + str);
// the last string of "
if ((j = str.indexOf("\"")) > 0) {
str = str.substring(0, j);
}
//now str is the file directory in the client pc
if ((j = str.lastIndexOf("\\")) != -1) {
filename = str.substring(j);
}
break;
}
}
}
catch (IOException ioe) {
System.out.println("ERROR");
System.out.println(ioe.getMessage());
} System.out.println("filename:" + filename + ".");
return filename;
}
}