问题描述
在web服务器可以上传但是在客户端却上传不了谁能帮忙解决一下谢谢了servlet类packagecom.controller.servlet;importjava.io.IOException;importjava.sql.ResultSet;importjava.sql.SQLException;importjavax.servlet.RequestDispatcher;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importsun.net.ftp.FtpClient;importutil.ConvertUtil;importutil.FtpUtil;importutil.ReadProperties;importcom.model.connection.MySQLConnection;importcom.model.service.dao.CategoryDao;importcom.model.service.dao.DaoFactory;importcom.model.service.pojo.Homework;importcom.model.service.pojo.Studentwork;/***ServletimplementationclassFtpServlet*/publicclassFtpServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;/***@seeHttpServlet#HttpServlet()*/publicFtpServlet(){super();//TODOAuto-generatedconstructorstub}/***@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)*/protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//TODOAuto-generatedmethodstubdoPost(request,response);}/***@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)*/protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//TODOAuto-generatedmethodstubHttpSessionsession=request.getSession();Longresult=0l;Stringcommander=request.getParameter("commander");Stringfilename=request.getParameter("filename");Stringaction=request.getParameter("action");Stringide=session.getAttribute("useride").toString();Stringurl=null;Stringmessage="";Stringerror="";StringenterURL="";StringftpServerIp=null;StringftpServerUser=null;StringftpServerPassword=null;FtpUtilftputil=newFtpUtil();FtpClientftpClient;ReadPropertiesreadProperties=ReadProperties.getInstance();if(commander.equals("studentWork")){StringswCONTENT=request.getParameter("sw_CONTENT");Studentworkstudentwork=newStudentwork();intuser_id=Integer.parseInt(session.getAttribute("userid").toString());inthw_id=Integer.parseInt(request.getParameter("hw_ID"));inttea_id=0;intcategory_id=0;Stringstu_name=null;Stringstu_no=null;Stringtea_no=null;Stringhw_title=null;Stringpath=null;ResultSetrs=null;//得到级别号教师id和作业标题rs=MySQLConnection.getInstance().query("SELECT*FROMhomeworkWHEREHW_ID="+hw_id);try{if(rs.next()){tea_id=rs.getInt("TEA_ID");category_id=rs.getInt("CATEGORY_ID");hw_title=rs.getString("HW_TITLE");rs.close();}}catch(SQLExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}//得到教师的编号rs=MySQLConnection.getInstance().query("SELECT*FROMteachersWHERETEA_ID="+tea_id);try{if(rs.next()){tea_no=rs.getString("TEA_NO");rs.close();}}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}//得到学生编号和名字rs=MySQLConnection.getInstance().query("SELECT*FROMstudentsWHERESTU_ID="+session.getAttribute("studentid"));try{if(rs.next()){stu_name=rs.getString("STU_NAME");stu_no=rs.getString("STU_NO");rs.close();}}catch(SQLExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}//得到年级和班级CategoryDaocategorydao=newCategoryDao();intcategory_id1=categorydao.getParent_category_id(category_id);Stringcategoryname1=categorydao.getName(category_id1);intcategory_id2=categorydao.getParent_category_id(category_id1);Stringcategoryname2=categorydao.getName(category_id2);path=tea_no+"/"+categoryname2+categoryname1+"/"+hw_title;//从配置文件中读取文件ftpServerIp=readProperties.getPro("ftpServerIp");ftpServerUser=readProperties.getPro("ftpServerUser");ftpServerPassword=readProperties.getPro("ftpServerPassword");if(filename!=null&&filename.length()!=0){ftpClient=ftputil.connectServer(path,ftpServerIp,ftpServerUser,ftpServerPassword);try{result=ftputil.upload(filename,stu_name,stu_no,ftpClient,ide);System.out.println}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}if(result>0){//上传成功error="文件上传成功。";Stringsuffix=filename.substring(filename.lastIndexOf("."),filename.length());//取得后缀名filename=path+"/"+stu_name+"_"+stu_no+suffix;studentwork.setSw_ADDURL(filename);}elseif(result==-1){error="要上传得文件丢失。";}else{error="没有上传附件。";}}studentwork.setHw_ID(hw_id);studentwork.setStu_ID(Integer.parseInt(session.getAttribute("studentid").toString()));studentwork.setSw_CONTENT(swCONTENT);studentwork.setUser_ID(user_id);if(action.equals("add")){if(DaoFactory.getStudentworkDao().create(studentwork)!=0){message="作业提交成功!"+error;enterURL="homeworkList.jsp";}else{message="遇到问题,请重新提交作业!"+error;enterURL="addHomework.jsp";}}elseif(action.equals("edit")){studentwork.setSw_ID(Integer.parseInt(request.getParameter("sw_ID")));studentwork.setSw_UPDATE_TIME(ConvertUtil.getInstance().getTime("yyyy-MM-ddHH:mm:ss"));studentwork.setSw_STU_TEA_CUR(request.getParameter("sw_STU_TEA_CUR"));if(DaoFactory.getStudentworkDao().update(studentwork)!=0){message="作业修改成功!"+error;enterURL="homeworkList.jsp";}}else{message="作业修改遇到问题,请重新修改!"+error;enterURL="home.do";}}request.setAttribute("message",message);request.setAttribute("url","home.do");url="message.jsp";RequestDispatcherdispatch=request.getRequestDispatcher(url);dispatch.forward(request,response);}}//上传文件类packageutil;importjava.io.FileInputStream;importjava.io.IOException;importjava.util.StringTokenizer;importsun.net.TelnetOutputStream;importsun.net.ftp.FtpClient;publicclassFtpUtil{FtpClientftpClient;publicFtpClientconnectServer(Stringpath,StringftpServerIp,StringftpServerUser,StringftpServerPassword)throwsIOException{//server:FTP服务器的IP地址;user:登录FTP服务器的用户名//password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径ftpClient=newFtpClient();ftpClient.openServer(ftpServerIp);ftpClient.login(ftpServerUser,ftpServerPassword);//登陆ftp服务器//path是ftp服务下主目录的子目录if(path.length()!=0)try{ftpClient.ascii();}catch(IOExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}StringTokenizers=newStringTokenizer(path,"/");//signintcount=s.countTokens();StringpathName="";while(s.hasMoreElements()){pathName=pathName+"/"+(String)s.nextElement();try{System.out.println("11111111111111111111111111111");ftpClient.sendServer("MKD"+pathName+"rn");System.out.println(pathName);}catch(Exceptione){System.out.println(e);e=null;}try{ftpClient.readServerResponse();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}try{ftpClient.cd(path);}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}ftpClient.binary();//用二进制上传returnftpClient;}publiclongupload(Stringfilename,Stringstu_name,Stringstu_no,FtpClientftpClient,Stringide){Stringnewname=null;longresult=0;TelnetOutputStreamos=null;FileInputStreamis=null;Stringsuffix=filename.substring(filename.lastIndexOf("."),filename.length());Stringtime=ConvertUtil.getInstance().getTime("yyyyMMddHHmmss");ConvertUtil.getInstance().setTime(time);if(ide.equals("teacher")){newname=stu_name+"_"+stu_no+"_"+time+suffix;}else{newname=stu_name+"_"+stu_no+suffix;}try{java.io.Filefile_in=newjava.io.File(filename);while(!file_in.exists()){return-1;}while(file_in.length()==0){return-2;}os=ftpClient.put(newname);result=file_in.length();is=newFileInputStream(file_in);byte[]bytes=newbyte[1024];intc;while((c=is.read(bytes))!=-1){os.write(bytes,0,c);}}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{try{if(is!=null){is.close();}if(os!=null){os.close();}closeServer();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}returnresult;}publicvoidcloseServer()throwsIOException{try{if(ftpClient!=null){ftpClient.closeServer();}}catch(IOExceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){}}
解决方案
解决方案二:
建议发到j2ee版块