jsp上传图片问题 在线等!!
表单页面
<HTML>
<HEAD>
<TITLE> Image File </TITLE>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
</HEAD>
<BODY>
<FORM METHOD=POST ACTION= "testimage.jsp ">
<INPUT TYPE= "text " NAME= "content "> <BR>
<INPUT TYPE= "file " NAME= "image "> <BR>
<INPUT TYPE= "submit "> </FORM>
</BODY>
</HTML>
动作页面:
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* " %>
<%@ page import= "java.util.* "%>
<%@ page import= "java.text.* "%>
<%@ page import= "java.io.* "%>
<%
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ").newInstance();
String url= "jdbc:odbc:test ";
String user= "sa ";
String password= "123456 ";
Connection conn=DriverManager.getConnection(url,user,password);
String content=request.getParameter( "content ");
String filename=request.getParameter( "image ");
FileInputStream str=new FileInputStream(filename);
String sql= "insert into test(id,content,image) values(1,?,?) ";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(2,content);
pstmt.setBinaryStream(3,str,str.available());
pstmt.execute();
out.println( "Success,You Have Insert an Image Successfully ");
%>
系统提示错误:
An error occurred at line: 6 in the jsp file: /testimage.jsp
Syntax error on token "Invalid Character ", delete this token
3: <%@ page import= "java.util.* "%>
4: <%@ page import= "java.text.* "%>
5: <%@ page import= "java.io.* "%>
6: <%
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ").newInstance();
7: String url= "jdbc:odbc:test ";
8: String user= "sa ";
9: String password= "123456 ";
我的数据库为SQL Server2000 test表的为id,content,image(为image类型)
我的数据库是 SQL SERVER2000 该怎么弄呢 系统提示错误的那行是我从我其他程序上复制来的 怎么那行在其他地方没事呢 应该不是那行的错误吧 是它关联的地方错了吧 希望有高人指点
[解决办法]
1、文件上传
FORM
要加入enctype= "multipart/form-data "
ACTION或SERVLET
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
基本方法如下:
String temp=request.getSession().getServletContext().getRealPath( "/ ")+ "temp "; //临时目录
String loadpath=request.getSession().getServletContext().getRealPath( "/ ")+ "Image "; //上传文件存放目录
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(1*1024*1024); // 设置允许用户上传文件大小,单位:字节
fu.setSizeThreshold(4096); // 设置最多只允许在内存中存储的数据,单位:字节
fu.setRepositoryPath(temp); // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
//开始读取上传信息
int index=0;
List fileItems = fu.parseRequest(request);
Iterator iter = fileItems.iterator(); // 依次处理每个上传的文件
while (iter.hasNext())
{
FileItem item = (FileItem)iter.next();// 忽略其他不是文件域的所有表单信息
if (!item.isFormField())
{
String name = item.getName();//获取上传文件名,包括路径
name=name.substring(name.lastIndexOf( "\\ ")+1);//从全路径中提取文件名
long size = item.getSize();
if((name==null||name.equals( " ")) && size==0)
continue;
int point = name.indexOf( ". ");
name=(new Date()).getTime()+name.substring(point,name.length())+index;
index++;
File fNew= new File(loadpath, name);
item.write(fNew);
}
else //取出不是文件域的所有表单信息
{
String fieldvalue = item.getString();
//如果包含中文应写为:(转为UTF-8编码)
//String fieldvalue = new String(item.getString().getBytes(), "UTF-8 ");
}
}
PS:同FORM的其他输入信息也将被当做数据流传输
不能使用STRUTS的ACTIONFORM
不能使用过滤类
2、图片预览
function previewA()
{
var x = document.getElementById( "signImage "); //singimage为FORM中已存在的IMAGE标签ID
if(!x || !x.value)
return;
var patn = /\.jpg$|\.jpeg$|\.gif$/i; //正则表达式,判断所选文件是否图片
if(patn.test(x.value))
{
var y = document.getElementById( "sign "); //sing为FORM存在的FILE标签ID
y.src = 'file://localhost/ ' + x.value;
var img=document.getElementById( 'signImage ');
img.setAttribute( 'width ', '120 ');
img.setAttribute( 'height ', '90 ');
}
else
{
alert( "您选择的似乎不是图像文件。 ");
}
}