文件上传问题(急!),下面是我从站点上找到的代码做了一些修改,但是没有成功,请各位大侠帮帮忙,帮我修改一下程序!(300分)

  • 主题发起人 主题发起人 Emily
  • 开始时间 开始时间
E

Emily

Unregistered / Unconfirmed
GUEST, unregistred user!
要求:从客户端上传HTML文件(结尾必须是.html格式) ,放到服务器目录下。以下是代码
代码:
                                  DataInputStream in=null;
                                   FileOutputStream fileOut=null;
                                   String contentType = request.getContentType();
                                   System.out.println("get ContentType:"+contentType);
                                   in = new DataInputStream(request.getInputStream());
                                   int formDataLength = request.getContentLength();
                                   byte dataBytes[] = new byte[formDataLength];
                                   int bytesRead = 0;
                                   String file = new String(dataBytes);
                                   System.out.println("String File:"+file);
                                   int lastIndex = contentType.lastIndexOf("=");
                                   String boundary = contentType.substring(lastIndex+1, contentType.length());
                                   System.out.println("lastIndex:"+lastIndex+"String boundary"+boundary);
                                   String directory="";
                                   if (file.indexOf("name=/"Directory/"") > 0){directory = file.substring(file.indexOf("name=/"Directory/""));
                                   directory = directory.substring(directory.indexOf("/n")+1);
                                   directory = directory.substring(directory.indexOf("/n")+1);
                                   directory = directory.substring(0,directory.indexOf("/n")-1);
                                   
                                    String saveFile = file.substring( file.indexOf("filename=/"")+10);
                                    System.out.println("saveFile:"+saveFile);
                                    if(saveFile.endsWith(".html"))//如果上载的文件是HTML则可以上传
                                    {
                                    saveFile = saveFile.substring(0,saveFile.indexOf("/n"));
                                    saveFile = saveFile.substring(saveFile.lastIndexOf("//")+1,saveFile.indexOf("/""));
                                    String rootPath="c://";
                                    String fileName = new String(rootPath +directory+saveFile);
                                    System.out.println("fileName:"+fileName);
                                    fileOut = new FileOutputStream(fileName);
                                    //write the string to the file as a byte array 
                                    fileOut.write(file.getBytes(),0,file.length());
                                    fileOut.close();
                                     }
 
www.jspsmart.com
 
有此类,你是否在文件开头引用了。把具体出错信息贴出来吧!
 
我已经实现了,用com.jspsmart.upload.SmartUpload 这个组件,不然处理很麻烦.
<%@ page contentType="text/html;charset=gb2312"%>
<%@ include file="../manager/program.jsp" %>
<%
com.jspsmart.upload.SmartUpload upload = new com.jspsmart.upload.SmartUpload();
upload.initialize(pageContext);
upload.upload();
upload.setTotalMaxFileSize(2147483647); //2M内
String userid = upload.getRequest().getParameter("userid");
String title = upload.getRequest().getParameter("title");
String bname = upload.getRequest().getParameter("bname");
if( mystr.isNull( bname ) || mystr.isNull(userid) || mystr.isNull(title) )
{
myprog.gotoUrl( "./index.jsp?op=list" );
}
String email = upload.getRequest().getParameter("email");
int re_id = new MyMath().Int( upload.getRequest().getParameter("reid") );
int iconid = new MyMath().Int( upload.getRequest().getParameter("iconid") );
String content = upload.getRequest().getParameter("content");
if( iconid == 0 || iconid > 31 ) iconid = 1;

//插入留言
String query = "INSERT INTO club_data SET userid='"+userid+"',title='"+title+"',content='"+content+"',date=now(),bbs_name='"+bname+"', re_id='"+re_id+"', re_num=0, iconid='" + iconid + "', email='" + email + "', view=0";
int rows = mysql.db_update( query );
if( rows == 0 )
{
myprog.gotoUrl("../error.htm");
}
//上传文件
if( upload.getFiles().getCount() > 0 )
{
String insertID = "0";
if( mysql.db_query( "SELECT last_insert_id()" ) > 0 );
{
insertID = mysql.getValue( 0, 0 );
}
if( ! insertID.equals("0") )
{
com.jspsmart.upload.File myFile = upload.getFiles().getFile(0);
if (!myFile.isMissing()) {
String path = myprog.getPath( "./upload/" );
String fname = path + insertID + "." + myFile.getFileExt();
myFile.saveAs( fname, upload.SAVE_PHYSICAL);
}
mysql.db_update( "UPDATE club_data SET filename='"+myFile.getFileName()+"' WHERE id='"+insertID+"'" );
}
}
//更新回复数
query = "UPDATE club_data SET re_num=re_num+1 WHERE id='"+re_id+"'";
mysql.db_update( query );
myprog.gotoUrl("./index.jsp?op=list&amp;bname="+bname);
%>
 
考下来研究研究
 
接受答案了.
 
后退
顶部