jsp文件上传得问题(如何用纯java代码实现文件的上传) 请教大家拉!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 文件上传没什么好说的啊 界面上设置一个 input type=file的 指定文件 然后 用formfile接收 action获取到了 写入磁盘 就ok了啊 想在jsp中嵌套纯java代码,可以通过底层的二进制流来取得上传的文件内容并将该文件放到web应用所在的路径,从而实现上传。如下列代码——》a.html->-----------------------------------------------------------------------------<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title> fileUpload </title> <meta name="author" content="Yeeku.H.Lee" /> <meta name="website" content="http://www.crazyit.org" /> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /></head><body><form enctype="multipart/form-data" method="post" action="b.jsp"> 上传文件: <input type="file" name="file" /><br> 请求参数 <input type="text" name="wawa" /><br> <input type="submit" value="提交" /></form></body></html>b.jsp->------------------------------------------------------------------<%@ page contentType="text/html;charset=GBK" %><%@ page import="java.io.*"%><%//取得HttpServletRequest的InputStream输入流InputStream is = request.getInputStream();//以InputStream输入流为基础,建立一个BufferedReader对象BufferedReader br = new BufferedReader(new InputStreamReader(is));String buffer = null;//循环读取请求内容的每一行内容while( (buffer = br.readLine()) != null){ if(buffer.endsWith("--") && buffer .startsWith("-----------------------------")) { break; } if(buffer.startsWith("-----------------------------")) { if (br.readLine().indexOf("filename") > 1) { //跳过两行,开始处理上传的文件内容 br.readLine(); br.readLine(); //以系统时间为文件名,创建一个新文件 File file = new File(request.getRealPath("/") + System.currentTimeMillis()); //创建一个文件输出流 PrintStream ps = new PrintStream(new FileOutputStream(file)); String content = null; //接着开始读取文件内容 while( (content = br.readLine()) != null) { if(content.startsWith("-----------------------------")) { break; } //将读到的内容输出到文件中 ps.println(content); } ps.flush(); ps.close(); } }}br.close();%> 关于hibernate search3.2.1的一些问题。。。 帮我解决一个异常 传值? filter中如何获得表单post的数据 struts中的tiles问题 一个关于jdbc操作sqlserver2005的问题 手机访问服务器的时候,服务器是如何判定是手机还是普通浏览器? 简单问题。在struts-config.xml加上连接数据库的语句就出现错误,百思不得其解? 提问:在Struts结构中我怎么样传对象?? 关于Socket 高手請進,Class.forName的問題,謝謝!!! asm3.0 如何动态生成代码,用过的进
a.html->-----------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> fileUpload </title>
<meta name="author" content="Yeeku.H.Lee" />
<meta name="website" content="http://www.crazyit.org" />
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
</head>
<body>
<form enctype="multipart/form-data"
method="post" action="b.jsp">
上传文件: <input type="file" name="file" /><br>
请求参数 <input type="text" name="wawa" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
b.jsp->------------------------------------------------------------------<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.io.*"%>
<%
//取得HttpServletRequest的InputStream输入流
InputStream is = request.getInputStream();
//以InputStream输入流为基础,建立一个BufferedReader对象
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String buffer = null;
//循环读取请求内容的每一行内容
while( (buffer = br.readLine()) != null)
{
if(buffer.endsWith("--") && buffer
.startsWith("-----------------------------"))
{
break;
}
if(buffer.startsWith("-----------------------------"))
{
if (br.readLine().indexOf("filename") > 1)
{
//跳过两行,开始处理上传的文件内容
br.readLine();
br.readLine();
//以系统时间为文件名,创建一个新文件
File file = new File(request.getRealPath("/")
+ System.currentTimeMillis());
//创建一个文件输出流
PrintStream ps = new PrintStream(new FileOutputStream(file));
String content = null;
//接着开始读取文件内容
while( (content = br.readLine()) != null)
{
if(content.startsWith("-----------------------------"))
{
break;
}
//将读到的内容输出到文件中
ps.println(content);
}
ps.flush();
ps.close();
}
}
}
br.close();
%>