java发送url请求进行文件的提交以及后台struts2的action接收处理
1、java模拟表单方式发送url请求进行文件的提交
/** * * @作者 王建明 * @创建日期 2013-06-27 * @创建时间 19:28:18 * @描述 —— 模拟表单进行文件数据提交 */ private static void testSimulateFormToPostFile() { String localPath = "F:\\软件开发经验\\DeleteNullDir.java"; File file = null;// 本地文件 URL url = null;// 服务器action地址 StringBuffer sb_cookie = null;// 拼装cookies StringBuffer sb_body = null;// 报文体 HttpURLConnection httpUrl = null;// http协议类 OutputStream fos = null;// 文件流 FileInputStream fis = null;// 服务器回写响应流 BufferedReader br = null;// 读取响应 try { file = new File(localPath); if (!file.exists()) { throw new Exception(); } String _url = http://10.49.61.101:9999/finance/ajaxUploadFile.do; // Cookie[] cs = request.getCookies(); // sb_cookie = new StringBuffer(); // for (Cookie c : cs) { // sb_cookie.append(" "); // sb_cookie.append(c.getName()); // sb_cookie.append("="); // sb_cookie.append(c.getValue()); // sb_cookie.append(";"); // } // String cookie = sb_cookie.substring(0, sb_cookie.length() - 1);// // cookie结束不含有";" String boundary = "---------------------------7da2e536604c8"; url = new URL(_url); httpUrl = (HttpURLConnection) url.openConnection();// 创建连接 httpUrl.setDoInput(true);// 创建输入流,必须有 httpUrl.setDoOutput(true);// 创建输出流,必须有 httpUrl.setUseCaches(false);// 不缓存 httpUrl.setConnectTimeout(30000);// 连接超时 httpUrl.setReadTimeout(30000);// 响应超时 httpUrl.setRequestMethod("POST"); httpUrl.setRequestProperty("Content-Length", "" + file.length());// 文件大小 httpUrl.addRequestProperty("Charset", "UTF-8"); httpUrl.addRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); httpUrl.addRequestProperty("Connection", "Keep-Alive");// 连接方式,此处为长连接 // httpUrl.addRequestProperty("Cookie", cookie);// 权限验证使用 fos = httpUrl.getOutputStream(); // 注意,http协议,是流传输,全部内容都要转换为byte类型 sb_body = new StringBuffer(); // 分隔符 sb_body.append("--"); sb_body.append(boundary); sb_body.append("\r\n"); // 文档类型 sb_body.append("Content-Disposition: form-data;name=\"upFile\";" + "filename=\"" + "upload_data.xlsx" + "\"\r\n"); sb_body.append("Content-Type:application/ms-word\r\n\r\n"); byte[] head = sb_body.toString().getBytes(); fos.write(head); // 文件内容 fis = new FileInputStream(file); byte[] read = new byte[2048]; int offset = 0; while ((offset = fis.read(read)) != -1) { fos.write(read, 0, offset); } fos.write(("\r\n--" + boundary + "--\r\n").getBytes()); fos.flush();// 发送请求 // HTTP响应 br = new BufferedReader(new InputStreamReader(httpUrl .getInputStream())); String line = null; StringBuffer sb = new StringBuffer(); while ((line = br.readLine()) != null) { sb.append(line); } System.out.println(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } |
2、java使用流的方式发送url请求进行文件的提交
/**
conn.setDoOutput(true); OutputStream out = conn.getOutputStream(); File file = new File("F:\\软件开发经验NumberFormateUtil.java>"); DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(conn |
3、后台服务端对应的struts2进行文件内容的接收处理【ajaxUploadFile和ajaxUploadFileTwo两个action的接收处理】
package com.eshopmates.finance.action;
import java.io.File; import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.Action;
/**
/**
/**
String filePath = request.getParameter("filePath");
InputStream input = request.getInputStream();
File file = new File(rootPath + filePath);
int size = 0;
public File getUpFile() {
public void setUpFile(File upFile) {
public String getUpFileContentType() {
public void setUpFileContentType(String upFileContentType) {
public String getUpFileFileName() {
public void setUpFileFileName(String upFileFileName) { |