社区
Web 开发
帖子详情
jsp图片上传的代码
yxqc
2004-10-18 09:36:13
怎么实现jsp图片上传到服务器的文件夹里?誰有这样的代码?
...全文
1779
7
打赏
收藏
微信扫一扫
点击复制链接
分享
下载分享明细
分享
举报
写回复
7 条
回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
yyqllxh2004
2004-10-18
http://www.eastkeyi.com/help.rar你的问题这个里面就有,去下载看代码
打赏
举报
回复
赞
边城狂人
2004-10-18
还是用 smartupload 好一些。
打赏
举报
回复
赞
yxqc
2004-10-18
先前我搜索了关于该问题的疑答,好象效果还不是很满意的。
我要求的功能是:
1、可以能判断要上传的图片格式,一般只判断JPG、GIF两种格式,其它的图片格式禁止上传;
2、限制图片的大小,一般超过200KB的图片禁止上传。
3、后台的数据库用的是mysql,不知道怎样存贮图片?是把图片上传至服务器上的指定的目录下,再由数据库存贮该图片的路径名?还是直接把图片存贮到数据库的字段内?
以上问题请各位大侠指点,能有代码指点最好,谢谢。
打赏
举报
回复
赞
yxqc
2004-10-18
我已经把jspsmartupload.jar放到Tomcat 4.1\shared\lib里
然后有如下两个文件:
upload.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head>
<title>文件上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<p> </p>
<p align="center">上传文件选择</p>
<FORM METHOD="POST" ACTION="do_upload.jsp"
ENCTYPE="multipart/form-data">
<input type="hidden" name="TEST" value="good">
<table width="75%" border="1" align="center">
<tr>
<td><div align="center">1、
<input type="FILE" name="FILE1" size="30">
</div></td>
</tr>
<tr>
<td><div align="center">2、
<input type="FILE" name="FILE2" size="30">
</div></td>
</tr>
<tr>
<td><div align="center">3、
<input type="FILE" name="FILE3" size="30">
</div></td>
</tr>
<tr>
<td><div align="center">4、
<input type="FILE" name="FILE4" size="30">
</div></td>
</tr>
<tr>
<td><div align="center">
<input type="submit" name="Submit" value="上传它!">
</div></td>
</tr>
</table>
</FORM>
</body>
</html>
do_upload.jsp
<%@ page contentType="text/html; charset=gb2312" language="java"
import="java.util.*,com.jspsmart.upload.*" errorPage="" %>
<html>
<head>
<title>文件上传处理页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
// 上传初始化
su.initialize(pageContext);
// 设定上传限制
// 1.限制每个上传文件的最大长度。
// su.setMaxFileSize(10000);
// 2.限制总上传数据的长度。
// su.setTotalMaxFileSize(20000);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
// su.setAllowedFilesList("doc,txt");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
jsp,htm,html扩展名的文件和没有扩展名的文件。
// su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
// 上传文件
su.upload();
// 将上传文件全部保存到指定目录
int count = su.save("/upload");
out.println(count+"个文件上传成功!<br>");
// 利用Request对象获取参数之值
out.println("TEST="+su.getRequest().getParameter("TEST")
+"<BR><BR>");
// 逐一提取上传文件信息,同时可保存文件。
for (int i=0;i<su.getFiles().getCount();i++)
{
com.jspsmart.upload.File file = su.getFiles().getFile(i);
// 若文件不存在则继续
if (file.isMissing()) continue;
// 显示当前文件信息
out.println("<TABLE BORDER=1>");
out.println("<TR><TD>表单项名(FieldName)</TD><TD>"
+ file.getFieldName() + "</TD></TR>");
out.println("<TR><TD>文件长度(Size)</TD><TD>" +
file.getSize() + "</TD></TR>");
out.println("<TR><TD>文件名(FileName)</TD><TD>"
+ file.getFileName() + "</TD></TR>");
out.println("<TR><TD>文件扩展名(FileExt)</TD><TD>"
+ file.getFileExt() + "</TD></TR>");
out.println("<TR><TD>文件全名(FilePathName)</TD><TD>"
+ file.getFilePathName() + "</TD></TR>");
out.println("</TABLE><BR>");
// 将文件另存
// file.saveAs("/upload/" + myFile.getFileName());
// 另存到以WEB应用程序的根目录为文件根目录的目录下
// file.saveAs("/upload/" + myFile.getFileName(),
su.SAVE_VIRTUAL);
// 另存到操作系统的根目录为文件根目录的目录下
// file.saveAs("c:\\temp\\" + myFile.getFileName(),
su.SAVE_PHYSICAL);
}
%>
</body>
</html>
当我上传图片的时候提示错误:
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 9 in the jsp file: /test/do_upload.jsp
Generated servlet error:
[javac] Compiling 1 source file
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:64: not a statement
jsp,htm,html?╁??????欢??病???灞?????浠躲??
^
An error occurred at line: 9 in the jsp file: /test/do_upload.jsp
Generated servlet error:
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:64: ';' expected
jsp,htm,html?╁??????欢??病???灞?????浠躲??
^
An error occurred at line: 9 in the jsp file: /test/do_upload.jsp
Generated servlet error:
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:64: illegal character: \12290
jsp,htm,html?╁??????欢??病???灞?????浠躲??
^
An error occurred at line: 9 in the jsp file: /test/do_upload.jsp
Generated servlet error:
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:102: not a statement
su.SAVE_VIRTUAL);
^
An error occurred at line: 9 in the jsp file: /test/do_upload.jsp
Generated servlet error:
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:102: ';' expected
su.SAVE_VIRTUAL);
^
An error occurred at line: 9 in the jsp file: /test/do_upload.jsp
Generated servlet error:
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:105: not a statement
su.SAVE_PHYSICAL);
^
An error occurred at line: 9 in the jsp file: /test/do_upload.jsp
Generated servlet error:
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:105: ';' expected
su.SAVE_PHYSICAL);
^
7 errors
打赏
举报
回复
赞
gjd111686
2004-10-18
<%
int iTotalByte,iTotalRead,iReadByte;
iTotalByte=request.getContentLength();
iTotalRead=0;
iReadByte=0;
byte[] Buffer=new byte[iTotalByte];
if(iTotalByte>0)
{
for(;iTotalRead<iTotalByte;iTotalRead+=iReadByte)
{
try
{
iReadByte=request.getInputStream().read(Buffer,iTotalRead,iTotalByte-iTotalRead);
}
catch(Exception e)
{
e.printStackTrace();
}
}
String strContentType=request.getContentType();
//数据处理开始
String strBuffer=new String(Buffer);
%><!--<br>表单数据:<br>strBuffer<br>--><%
String strBoundary="--"+strContentType.substring(strContentType.lastIndexOf("=")+1,strContentType.length());
String strArray[]=strBuffer.split(strBoundary);
String strSubString;
int iBegin,iEnd;
iBegin=0;iEnd=0;
String strFieldName="";
String strFieldValue="";
String strFilePath="";
String strFileName="";
String strFileType="";
boolean bTrue;
bTrue=false;
int iLocation=0;
for(int iIndex=1;iIndex<strArray.length-1;iIndex++)
{
strSubString=strArray[iIndex];
iBegin=strSubString.indexOf("name=\"",0);
if(iBegin!=-1)
{
strFieldName="";strFieldValue="";
strFilePath="";strFileName="";strFileType="";
iEnd=strSubString.indexOf("\"",iBegin+6);
strFieldName=strSubString.substring(iBegin+6,iEnd);
iBegin=strSubString.indexOf("filename=\"",0); if(iBegin!=-1)
{
bTrue=true;
}
iEnd=strSubString.indexOf("\r\n\r\n",0);
if(bTrue==true)
{
//文件路径
strFilePath=strSubString.substring(iBegin+10,strSubString.indexOf("\"",iBegin+10));strFileName=strFilePath.substring(strFilePath.lastIndexOf("\\")+1);
strFileType=strSubString.substring(strSubString.indexOf("Content-Type: ")+14,strSubString.indexOf("\r\n\r\n"));
%><!--<br>文件类型:<br>strFileType<br>--><%
//文件数据
iBegin=strSubString.indexOf("\r\n\r\n",iBegin);
strFieldValue=strSubString.substring(iBegin+4);
strFieldValue=strFieldValue.substring(0,strFieldValue.lastIndexOf("\n")-1);
%><!--<br>文件路径:<br>strFilePath<br>文件名称:<br>strFileName<br>--><%
byte[] pFile=strFieldValue.getBytes();
byte[] pFileExtend=new byte[pFile.length];
iLocation=strBuffer.indexOf("filename=\"",iLocation);
for(int kIndex=iLocation;kIndex<iTotalByte-2;kIndex++)
{
if(Buffer[kIndex]==13&&Buffer[kIndex+2]==13)
{iLocation=kIndex+4;break;}
}
for(int nIndex=0;nIndex<pFile.length;nIndex++)
{
pFileExtend[nIndex]=Buffer[iLocation+nIndex];
}
/*
//保存到Local Disk;
FileOutputStream pFileOutputStream=new FileOutputStream("F:\\Site_App\\UploadFile\\"+strFileName);
pFileOutputStream.write(pFileExtend);
pFileOutputStream.close();
*/
session.putValue(strFieldName+"_FileType",strFileType);
session.putValue(strFieldName+"_FilePath",strFilePath);
session.putValue(strFieldName+"_FileName",strFileName);
session.putValue(strFieldName,pFileExtend);
}
else
{
strFieldValue=strSubString.substring(iEnd+4);
strFieldValue=strFieldValue.substring(0,strFieldValue.lastIndexOf("\n")-1);
session.putValue(strFieldName,strFieldValue);
}
bTrue=false;
}
%><!--<br>表单域名:<br>strFieldName<br>表单域值:<br>strFieldValue<br>--><%
}
//数据处理结束
}
%>
这样(String)session.getValue("表单域名")返回表单域值,而(byte[])session.getValue("File上传控件域名")返回的字节数组就可以用new ByteArrayInputStream(byte[])调用updateBinaryStream来更新到数据库了
打赏
举报
回复
赞
jerrykey
2004-10-18
你可以去下载smartupload。很好用,干什么费尽自己写代码呢?
打赏
举报
回复
赞
gjd111686
2004-10-18
http://blog.csdn.net/gjd111686/archive/2004/08/18/78324.aspx
打赏
举报
回复
赞
相关推荐
jsp
图片
上传
代码
jsp
图片
上传
代码
jsp
图片
上传
代码
jsp
图片
上传
代码
jsp
java
上传
图片
_如何在
Jsp
上传
图片
1. 新建一个Dynamic Web Project:2. 键入工程名UploadImage:3. 选择Dynamic web module version :2.54.... 新建一个类,专门用来处理
图片
上传
新建一个包mytest:在包下新建一个类FileUpload:
代码
内...
jsp
图片
上传
代码
,
jsp
单张
图片
上传
上传
// 新建一个SmartUpload对象 SmartUpload su = new SmartUpload(); //
上传
初始化 su.initialize(pageContext); // 限制每个
上传
文件的最大长度。 su.setMaxFileSize(10000); // 限制总
上传
数据的长度。 su...
用
jsp
实现简单的
图片
上传
功能
用
jsp
实现简单的
图片
上传
功能
Windows 10远程桌面服务配置文件rdpwrap.10.0.19041.1741.zip
Windows远程桌面服务RDPWrap配置文件,适用于Windows 10 10.0.19041.1741及之前部分版本。 此配置文件只能支持Windows 10 10.0.19041.1741及之前部分版本,由于Windows不断更新版本,可能会导致rdpwrap配置文件不支持最新的Windows版本,请谨慎下载。 本账号会不定期更新支持最新Windows 10版本的rdpwrap配置文件,高于10.0.19041.1741版本的Windows 10用户,请加粉关注以获取最新的rdpwrap配置文件。
发帖
Web 开发
微信扫一扫
点击复制链接
分享社区
下载分享明细
8.0w+
社区成员
34.1w+
社区内容
Java Web 开发
社区管理员
加入社区
帖子事件
创建了帖子
2004-10-18 09:36
社区公告
暂无公告