在applet中把要上传或者下载的文件名传给servlet,然后由servlet处理是不是更简单呢?

解决方案 »

  1.   

    写入流中:
    servlet:
    ObjectInputStream ois=new ObjectInputStream(urlcon.getInputStream());
    Object o = (Object)ois.readObject();applet:
    URL url = new URL("servlet address?arguement=...")
    URLConnection urlcon = url.openConnection();
    urlcon.connect();
    String fPath = "文件地址";
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(fPath)));
    oos.writeObject(p);
      

  2.   

    servlet address?arguement=...这个可以传参,用?传~~~经常用的那种
      

  3.   

    呵呵大致这个流程我也知道,但是我的代码好像有问题,Servlet没有任何的反应(重要的是连保存信息都没有,我怀疑是根本没有被呼叫),而applet里面所有返回值都是正确的我在外面法帖问的,手上没有代码,故没有办法贴原始的代码
      

  4.   

    Serializable序列化的问题,就是把类写入到流中~~这样就可以把类传到任何地方,你可以去看看这方面的书
      

  5.   

    这是一个文件上传的Servlet程序,不用组件完全能实现文件的上传。有不懂得地方,可以提问!关注!!!
    //*****************************************************************
    package ***
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    import bee.*;
    public class chapteraddfile extends HttpServlet {
      private static final String CONTENT_TYPE = "text/html; charset=GBK";
       static final int MAX_SIZE=102400*102400;
       String rootPath;
      //Initialize global variables
      public void init() throws ServletException {
      }
      //Process the HTTP Post request
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("GBK");
        String name=request.getParameter("tfName");
        String chapterId=request.getParameter("chapterId");
        String returnUrl=request.getParameter("returnUrl");
           DataInputStream in=null;
          FileOutputStream fileOut=null;
            String realPath=this.getServletContext().getRealPath(this.getServletName());
        realPath=realPath.substring(0,realPath.lastIndexOf("\\"));
          rootPath=realPath+ "\\upload\\";
        try
        {
                 String contentType=request.getContentType();
               if (contentType.indexOf("multipart/form-data")>=0)
          {
                  in=new DataInputStream(request.getInputStream());
                  int formDataLength=request.getContentLength();
                  if(formDataLength>MAX_SIZE)
            {
              out.println("<p>上传的字节数不可以超过" + MAX_SIZE + "</p>");
              return;
            }
                 byte dataBytes[]=new byte[formDataLength];
            int byteRead=0;
            int totalBytesRead=0;
                 while(totalBytesRead<formDataLength)
            {
              byteRead=in.read(dataBytes,totalBytesRead,formDataLength);
              totalBytesRead+=byteRead;
            }
                  String file=new String(dataBytes);
          
            chapterId=file.substring(file.indexOf("@*@*@*@*@*")+10,file.lastIndexOf("@*@*@*@*@*"));
            returnUrl=file.substring(file.indexOf("*@*@*@*@*@")+10,file.lastIndexOf("*@*@*@*@*@"));
            String deleteStart=file.substring(file.indexOf("-----------------------------")+30);
            name=deleteStart.substring(deleteStart.indexOf("tfName")+7,deleteStart.indexOf("-----------------------------"));
            String saveFile=file.substring(file.indexOf("filename=\"")+10);
            saveFile=saveFile.substring(0,saveFile.indexOf("\n"));
            saveFile=saveFile.substring(saveFile.lastIndexOf("\\")+1,saveFile.indexOf("\""));
             int lastIndex=contentType.lastIndexOf("=") ;
                   String boundary=contentType.substring(lastIndex+1,contentType.length());
                  String fileStyle=saveFile.substring(saveFile.indexOf(".")+1);
                    Random rdm = new Random();
            int rdmNum = rdm.nextInt(10000);
            Random rdm1 = new Random();
            int rdmNum1 = rdm1.nextInt(10000);
             String theFileName="Ben"+rdmNum1+rdmNum;
                    saveFile=theFileName + "." + fileStyle;
                    String url="upload/";
           
             String fileName=rootPath+saveFile;
            int pos;
             pos=file.indexOf("filename=\"");
             pos=file.indexOf("\n",pos)+1;
             pos=file.indexOf("\n",pos)+1;
             pos=file.indexOf("\n",pos)+1;
             int boundaryLocation=file.indexOf(boundary,pos)-4;
                     int startPos=((file.substring(0,pos)).getBytes()).length;
                    int endPos=((file.substring(0,boundaryLocation)).getBytes()).length;
                   File fileDir=new File(rootPath);
             if(!fileDir.exists())
             {
               fileDir.mkdirs();
             }
                     fileOut=new FileOutputStream(fileName);
                  fileOut.write(dataBytes,startPos,(endPos-startPos));
             fileOut.close();
                     String insertSubjectFile="insert into subjectfile (name,preid,url,style) values ('" + name + "'," + chapterId + ",'" + url + "','" + fileStyle + "')";
            // oracledbaction oracledbaction=new oracledbaction();
             //oracledbaction.executeAction(insertSubjectFile);
             bee.DBconn db = new bee.DBconn();
             db.executeUpdate(insertSubjectFile);
             response.sendRedirect(returnUrl);      }
          else
          {
            String content=request.getContentType();
            out.println("<p>上传的数据类型不是multipart/form-data</p>");
          }    }
        catch(Exception ex)
        {
           ex.printStackTrace();
        }
      }
      //Clean up resources
      public void destroy() {
      }
    }
      

  6.   

    没有看明白现在我遇到的最大的问题是applet好像根本没有调到servlet;applet的所有调试信息都没有出错URL url = new URL("http://200.1.1.189:8080/servlet/SaveFile");
    URLConnection urlCon = url.openConnection();
    urlCon.setDoOutput(true);
    urlCon.setDoInput(true);
    urlCon.setAllowUserInteraction(false);
    //urlCon.connect(); //这个需要吗?有什么问题?
    那位那否给一个完整的示例,传文件+其他参数,文件名和参数的值不需要在applet里面管,applet里面的方法在调用的时候由调用者指定谢谢
      

  7.   

    URL url = new URL("http://200.1.1.189:8080/servlet/SaveFile");
    URLConnection urlCon = url.openConnection();
    urlCon.setDoOutput(true);
    urlCon.setDoInput(true);
    urlCon.setRequestProperty("Content-type", "application/octest-stream");传送方法
    /**
     * 给Servlet传送对象
     * @param output
     * @param obj
     * @throws IOException
     */
    public void sendServletObject(Object obj) throws IOException {
    OutputStream output=con.getOutputStream();//con==urlCon
    ObjectOutputStream dataout = new ObjectOutputStream(output);
    dataout.writeObject(obj);
    dataout.flush();
    dataout.close();
    }
    思路:使用对象流,把一个对象传送给servlet.传送的这个对象可包括其他对象,比如输入流.
      

  8.   

    呵呵,用下面的模式去实验一下,应该可以实现的,先实验不传递参数的,然后在看传递参数的,好象传递参数有问题的,需要转化一下的,或者用流来传递,就是用xml数据包
    public static void main(String args[]) {
            try {
                //建立双向通信连接
                URL mtUrl = new URL("http://200.1.1.189:8080/servlet/SaveFile");
                URLConnection myCon = mtUrl.openConnection();
                myCon.setDoOutput(true);
                //发送数据包
                OutputStream outs = myCon.getOutputStream();
                String xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                                 
                System.out.println("==========xml信息"+xmlStr);
                outs.write(xmlStr.getBytes());
                outs.flush();
                outs.close();
                System.out.println("Send First finish.");
                System.out.println(
                    "=====waiting for server to cope with the data!=====");
                InputStream bin = myCon.getInputStream();
                System.out.println(
                    "========server finished the datacoping============");
                int length = 0;
                length = myCon.getContentLength();
                byte[] tempStr = new byte[length];
                int res = 0;
                bin.read(tempStr);
                bin.close();
                System.out.println("read ok. length = " + length);
                System.out.println(new String(tempStr));
                System.out.println("========OK.==========");
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
      

  9.   

    能不能给我一个完整的代码不好意思,我对Java的IO是晕的哈或者QQ:4443183聊