java模拟表单上传资料
java模拟表单上传文件
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; /** * 文件上传工具 * @author cc * */ public class UploadTools { /** * 上传本地文件 * @param listPath * @param urlPath * @return */ public static String upload(List<String> listPath,String urlPath){ //返回数据 StringBuffer msg = new StringBuffer(); try { String BOUNDARY = "---------7d4a6d158c9"; // 定义数据分隔线 URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); OutputStream out = new DataOutputStream(conn.getOutputStream()); byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线 int leng = listPath.size(); for(int i=0;i<leng;i++){ String fname = listPath.get(i); File file = new File(fname); StringBuilder sb = new StringBuilder(); sb.append("--"); sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition: form-data;name=\"file"+i+"\";filename=\""+ file.getName() + "\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n"); byte[] data = sb.toString().getBytes(); out.write(data); DataInputStream in = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } out.write("\r\n".getBytes()); //多个文件时,二个文件之间加入这个 in.close(); } out.write(end_data); out.flush(); out.close(); // 定义BufferedReader输入流来读取URL的响应 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { msg.append(line); } } catch (Exception e) { e.printStackTrace(); } return msg.toString(); } /** * 上传网络文件 * @param listPath * @param urlPath * @return */ public static String upload(String fileName,String urlPath,byte[] bytesFile){ //返回数据 StringBuffer msg = new StringBuffer(); try { String BOUNDARY = "---------7d4a6d158c9"; // 定义数据分隔线 URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); OutputStream out = new DataOutputStream(conn.getOutputStream()); byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();// 定义最后数据分隔线 // 数据流部分 // StringBuilder sb = new StringBuilder(); sb.append("--"); sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition: form-data;name=\"file\";filename=\""+ fileName + "\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n"); byte[] data = sb.toString().getBytes(); out.write(data); //写入文件 out.write(bytesFile, 0, bytesFile.length); out.write("\r\n".getBytes()); //多个文件时,二个文件之间加入这个 // 数据流部分 // out.write(end_data); out.flush(); out.close(); // 定义BufferedReader输入流来读取URL的响应 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { msg.append(line); } } catch (Exception e) { e.printStackTrace(); } return msg.toString(); } public static void main(String[] args){ List<String> list = new ArrayList<String>(); list.add("C:\\Hydrangeas.jpg"); UploadTools.upload(list,"http://127.0.0.1/upload/uploadData.do"); } }