Servlet下得不到参数

贪睡的兔子 2012-05-04 12:04:16
我客户端用

public static String sendRequest(String url, Map<String, String> params,
Map<String, File> files) throws Exception {
HttpURLConnection conn = (HttpURLConnection) new URL(url)
.openConnection();
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "\r\n";
String MULTIPART_FROM_DATA = "application/octet-stream";
String CHARSET = "UTF-8";

conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA
+ ";boundary=" + BOUNDARY);

// 首先组拼文本类型的参数

StringBuilder sb = new StringBuilder();
System.out.println(url);
DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());

// outStream.write(sb.toString().getBytes());
// 发送文件数据

if (files != null)
for (Map.Entry<String, File> file : files.entrySet()) {
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\""
+ file.getKey() + "\"" + LINEND);
sb1.append("Content-Type: application/octet-stream; charset="
+ CHARSET + LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes());
InputStream is = new FileInputStream(file.getValue());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
outStream.write(LINEND.getBytes());
}
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
outStream.write(end_data);
outStream.flush();

int res = conn.getResponseCode();
System.out.println("response Code==="+res);
if (res == 200) {
InputStream in = conn.getInputStream();
int ch;
StringBuilder sb2 = new StringBuilder();
BufferedReader bw=new BufferedReader(new InputStreamReader(in,CHARSET));
String line="";

while((line=bw.readLine())!=null){
sb2.append(line);
}
System.out.println(sb2);
in.close();
return sb2.toString();
}

outStream.close();
conn.disconnect();
return null;
}



向服务器 发数据 服务器用servlet 得不到 添加的参数
改用struts能直接得到添加的参数, 方法应该没问题,就是servlet下 加的参数全部得不到 ,是不是要另外解析的
...全文
154 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
有爱的小止 2012-05-22
  • 打赏
  • 举报
回复
Servlet要一个HttpServletRequest request的Request对象
然后取网址传过来的参数用request.getParameter("name");
xiaozhou10 2012-05-17
  • 打赏
  • 举报
回复
我也想求解 我的是url自动生成的后面加?page=* 然后servlet 都得不到 后来我在页面用个 隐藏的name属性
贪睡的兔子 2012-05-04
  • 打赏
  • 举报
回复
	InputStream inputStream=request.getInputStream();
BufferedReader buf=new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
String line="";
while((line=buf.readLine())!=null)
System.out.println(line);


得到的流能打印

--27693a33-29ab-4045-b3a6-f3b716f28a0d
Content-Disposition: form-data; name="pageNo"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

1
--27693a33-29ab-4045-b3a6-f3b716f28a0d
Content-Disposition: form-data; name="userId"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

48
--27693a33-29ab-4045-b3a6-f3b716f28a0d
Content-Disposition: form-data; name="pageSize"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

10
--27693a33-29ab-4045-b3a6-f3b716f28a0d
Content-Disposition: form-data; name="type"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

74
--27693a33-29ab-4045-b3a6-f3b716f28a0d
Content-Disposition: form-data; name="friendId"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

101
--27693a33-29ab-4045-b3a6-f3b716f28a0d--


但直接用request.getParamter("userId") 就得不到参数
贪睡的兔子 2012-05-04
  • 打赏
  • 举报
回复
传参数那块

StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet())
{
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINEND);
sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
sb.append(LINEND);
sb.append(entry.getValue());
sb.append(LINEND);
}

DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
outStream.write(sb.toString().getBytes());

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧