小弟这里有一个自己写的文件上传的code,不知道你是不是用的上哈,你自己看一下:
UploadServlet.java:import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UploadServlet extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//取得bundary
String contentType = request.getHeader("Content-Type");
String bundary = null;
if(contentType != null && contentType.startsWith("multipart/form-data;") ){
bundary = "--" + contentType.substring("multipart/form-data; boundary=".length());
}
ServletInputStream sis = null;
OutputStream os = null;
try{
//对body进行处理
sis = request.getInputStream();
byte[] buf = new byte[1024];
int len = -1;
sis.readLine(buf, 0, 1024); //bundary行
//取得文件名
len = sis.readLine(buf, 0, 1024); //Content-Disposition行
String fileNameLine = new String(buf, 0, len, "GB18030");
String fileName = fileNameLine.substring(fileNameLine.lastIndexOf("\\")+1, fileNameLine.length()-3);
System.out.println(fileName);
sis.readLine(buf, 0, 1024); //Content-Type行
sis.readLine(buf, 0, 1024); //文件内容之前的空行
//把文件内容写出去
os=new FileOutputStream(new File("D:\\upload\\" + fileName));
//以下注释的四行是为了解决文本文件多一空行的问题
//len = sis.readLine(buf, 0, 1024);
//os.write(buf, 0, len - 2);
while( (len = sis.readLine(buf, 0, 1024)) != -1){
String line = new String(buf, 0, len, "GB18030");
if(!line.startsWith(bundary)){
//os.write("\r\n".getBytes());
//os.write(buf, 0, len - 2);
os.write(buf, 0, len);
}else{
break;
}
}
}finally{
os.close();
sis.close();
}
}
}这是访问的html页面:index.html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=GB18030">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
please select your file to upload<br>
<form action="UploadServlet" method="post" enctype="multipart/form-data">
<input type="file" name="file01"><br>
<input type="submit" value="上传">
</form>
</body>
</html>web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>UploadServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
大概就是这样了,至于存数据库的工作就你自己在做了,D:\\upload这个文件你得先创建出来,这样就可以了。
UploadServlet.java:import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UploadServlet extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//取得bundary
String contentType = request.getHeader("Content-Type");
String bundary = null;
if(contentType != null && contentType.startsWith("multipart/form-data;") ){
bundary = "--" + contentType.substring("multipart/form-data; boundary=".length());
}
ServletInputStream sis = null;
OutputStream os = null;
try{
//对body进行处理
sis = request.getInputStream();
byte[] buf = new byte[1024];
int len = -1;
sis.readLine(buf, 0, 1024); //bundary行
//取得文件名
len = sis.readLine(buf, 0, 1024); //Content-Disposition行
String fileNameLine = new String(buf, 0, len, "GB18030");
String fileName = fileNameLine.substring(fileNameLine.lastIndexOf("\\")+1, fileNameLine.length()-3);
System.out.println(fileName);
sis.readLine(buf, 0, 1024); //Content-Type行
sis.readLine(buf, 0, 1024); //文件内容之前的空行
//把文件内容写出去
os=new FileOutputStream(new File("D:\\upload\\" + fileName));
//以下注释的四行是为了解决文本文件多一空行的问题
//len = sis.readLine(buf, 0, 1024);
//os.write(buf, 0, len - 2);
while( (len = sis.readLine(buf, 0, 1024)) != -1){
String line = new String(buf, 0, len, "GB18030");
if(!line.startsWith(bundary)){
//os.write("\r\n".getBytes());
//os.write(buf, 0, len - 2);
os.write(buf, 0, len);
}else{
break;
}
}
}finally{
os.close();
sis.close();
}
}
}这是访问的html页面:index.html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=GB18030">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
please select your file to upload<br>
<form action="UploadServlet" method="post" enctype="multipart/form-data">
<input type="file" name="file01"><br>
<input type="submit" value="上传">
</form>
</body>
</html>web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>UploadServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
大概就是这样了,至于存数据库的工作就你自己在做了,D:\\upload这个文件你得先创建出来,这样就可以了。
解决方案 »
- 求spring security cas单点登陆和tcl目录的相关资源
- 从xml里面得到一个对象,得到后应该如何起接收,如何解析出来
- 谁对网络硬盘比较了解
- 如何修改表中的数据如何设置Where条件
- EJB Message Driven Bean
- 求助:jsp 显示本地磁盘上的bmp图片?????????????
- 兄弟们问一个关于ActionForm中的数据传递
- jbuilder9里编译出现下面错误,不解。
- 请问大家在项目中愿意选择CMP还是JDO呢?
- 急!!!java中怎样 将int型转为string型???谢谢
- 求struts+hibernate做的购物网站源代码
- hibernate级联映射的问题
得到REALPATH应该不成问题.在FORMFILE里得到文件名,取得文件的后缀(.JPG),在前面加上表单提交过来的文件名.两者加一起就是路径了.IO流应该不难.