下面的是一个类里面的方法:
public static void inputList(String path) {
List<Map<String, byte[]>> datas = getTreeChild(path);
URL url;
try {
System.out.println("开始发送.....");
for (Map<String, byte[]> map : datas) {
Set<String> string = map.keySet();
for (String pathInfo : string) {
String sendPath = getExtName(pathInfo);
// System.out.println(sendPath);
// System.out.println(pathInfo);
url = new URL(actionUrl);
URLConnection con = url.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) con;
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setRequestProperty("Content-type",
"application/x-java-serialized-object");
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.connect();
OutputStream outStrm = httpUrlConnection.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outStrm);
oos.writeObject(sendPath);
oos.writeObject(datas);
oos.flush();
oos.close();
InputStream inStrm = httpUrlConnection.getInputStream();// String message = httpUrlConnection.getResponseMessage();
// System.out.println(message);
}
}
System.out.println("发送完毕");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}、、、、、、、、、、、、、、、、、
servlet里面的内容是:
package test;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Send extends HttpServlet {
private static final long serialVersionUID = 1L; @SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
InputStream inStream = request.getInputStream();
ObjectInputStream objInStream = new ObjectInputStream(inStream);
List<Map<String, byte[]>> list;
try {
// 接收传过来的集合
String oldPath = (String) objInStream.readObject();
String newPath = "E:" + oldPath;
File newDir = new File(newPath);
if (newDir.exists()) {
// System.out.println("传过来的文件已经存在");
// response.getWriter().write("123");
} else {
list = (List<Map<String, byte[]>>) objInStream.readObject();
System.out.println("开始接收数据");
for (Map<String, byte[]> map : list) {
Set<String> string = map.keySet();
for (String path : string) {
String oled = path;
// System.out.println(oled);
// H
String pathDir = oled.toString();
int bb = pathDir.lastIndexOf(":");
String dirPaht = pathDir.substring(0, bb);
// System.out.println(dirPaht);
// 得到文件名前面的路径
path = path.replace(dirPaht, "E");
String beforName = path.toString();
int b = beforName.lastIndexOf("\\");
String bName = beforName.substring(0, b);
 System.out.println(bName);
File dir = new File(bName);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(path);
OutputStream outputStream = new FileOutputStream(file);
outputStream.write(map.get(oled), 0,
map.get(oled).length);
}
}
System.out.println("接收数据完毕");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
、、、、、、、、、、、、
怎么传个字符串在上面的方法里面去接收

解决方案 »

  1.   

    这是啥问题??包名.类名.inputList("字符串");
      

  2.   

    到底是servlet里传个参数给上面的inputList()方法?还是inputList接收servlet的返回值?
    前者直接在servlet里掉,后者你应该没法用main直接得到,你可以把返回值(参数)传到另外一个servlet里,在另外的servlet里调用inputList方法,参数是上一个servlet传过来的
      

  3.   

    你main 和 servlet 怎么扯上关系呢。
      

  4.   

    servlet可以接收main方法传过来的参数,我的意思就是从servlet返回一个标示,也就是返回一个字符串,然后在main方法里面得到这个字符串,根据返回的值在做判断
      

  5.   

    我把main里面的代码都发出来:主要功能就是实现客户端把附件发送到客户端,然后接收存到指定的路径
    package com.phwz.client;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Set;import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;public class test {
    private static String actionUrl = "http://localhost:8080/Test/Send"; public static void main(String[] args) {
    String startPath = null;
    System.out.print("请输入磁盘路径:");
    Scanner sc = new Scanner(System.in);
    String tem = sc.nextLine();// TODO 磁盘路径要根据实际文件夹 的位置输入
    if (!(tem.equals("")))
    startPath = tem;
    inputList(startPath);
    } /**
     * 运用递归得到节点数据(文件夹名称和文件名称)
     * 
     * @param parentPath
     */
    private static List<Map<String, byte[]>> getTreeChild(String parentPath) {
    File file = new File(parentPath);
    List<Map<String, byte[]>> list = new ArrayList<Map<String, byte[]>>();
    try {
    list = findFiles(list, file);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return list;
    } /**
     * 递归查询所有的文件,并把它读进来然后放到集合里
     * 
     * @return
     */
    private static List<Map<String, byte[]>> findFiles(
    List<Map<String, byte[]>> list, File file) {
    try {
    if (file.isFile()) {// 文件
    InputStream inputStream = null;
    try {
    // (int) file.length()
    byte[] fileDatas = new byte[1024 * 5];
    inputStream = new FileInputStream(file);
    inputStream.read(fileDatas);
    Map<String, byte[]> map = new HashMap<String, byte[]>();
    map.put(file.getPath(), fileDatas);
    list.add(map);

    } finally {
    if (null != inputStream) {
    inputStream.close();
    inputStream = null;
    }
    }
    } else {
    File[] paths = file.listFiles();
    for (File childFile : paths) {
    findFiles(list, childFile);
    }
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException exception) {
    exception.printStackTrace();
    }
    return list;
    } /*
     * 发送到指定的servlet
     */
    public static void inputList(String path) {
    List<Map<String, byte[]>> datas = getTreeChild(path);
    URL url;
    try {
    System.out.println("开始发送.....");
    for (Map<String, byte[]> map : datas) {
    Set<String> string = map.keySet();
    for (String pathInfo : string) {
    String sendPath = getExtName(pathInfo);
    // System.out.println(sendPath);
    // System.out.println(pathInfo);
    url = new URL(actionUrl);
    URLConnection con = url.openConnection();
    HttpURLConnection httpUrlConnection = (HttpURLConnection) con;
    httpUrlConnection.setUseCaches(false);
    httpUrlConnection.setDoOutput(true);
    httpUrlConnection.setDoInput(true);
    httpUrlConnection.setRequestProperty("Content-type",
    "application/x-java-serialized-object");
    httpUrlConnection.setRequestMethod("POST");
    httpUrlConnection.connect();
    OutputStream outStrm = httpUrlConnection.getOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(outStrm);
    oos.writeObject(sendPath);
    oos.writeObject(datas);
    oos.flush();
    oos.close();
    InputStream inStrm = httpUrlConnection.getInputStream();// String message = httpUrlConnection.getResponseMessage();
    // System.out.println(message);
    }
    }
    System.out.println("发送完毕");
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    } public static String getExtName(String fileName) {
    if (fileName == null || fileName.equals("")) {
    return null;
    }
    if (fileName.contains(":")) {
    String[] arrFileName = fileName.split("\\:");
    return arrFileName[arrFileName.length - 1].toLowerCase();
    }
    return fileName;
    }
    }
      

  6.   

      网络编程 ?
    http ,socket ,webservice ?客户端到服务端