jsp上传文件用 servlet
<form name="form1" method="post" action="photoservlet" enctype="multipart/form-data">
<table width="309" height="229" align="center">
<tr>
<td width="106">姓名:</td>
<td width="240"><input type="text" name="name"></td>
</tr>
<tr>
<td height="47">专业:</td>
<td><input type="text" name="type"></td>
</tr>
<tr>
<td height="48">照片:</td>
<td><input type="file" name="photo" ></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="添加">
<input type="reset" name="Submit2" value="重置"></td>
</tr>
</table>
</form>在Servlet中
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.commons.fileupload.*;public class photoServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private String uploadPath = "image\\doctor\\";
private String tempPath = "image\\doctor\\"; // 用于存放临时文件的目录
//Initialize global variables
public void init() throws ServletException {
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk"); } //Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
try {
if(!new File(uploadPath).isDirectory()){
new File(uploadPath).mkdirs();
}
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(4194304);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4096);
// 设置临时目录:
fu.setRepositoryPath(tempPath);
// 得到所有的文件:
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
// 获得文件名,这个文件名包括路径:
String fileName = fi.getName();
if (fileName != null) {
fi.write(new File(uploadPath
+ "\\" + fileName.substring(fileName.lastIndexOf("\\") + 1)));
String name = request.getParameter("name");
System.out.print(name);
}
}
response.sendRedirect("adddoctor.jsp");
} catch (Exception e) {
// 可以跳转出错页面
}
} //Clean up resources
public void destroy() {
}
}同时我要把jsp中的信息写如数据库 应该怎么样实现哪位高手是否可以详细讲解
<form name="form1" method="post" action="photoservlet" enctype="multipart/form-data">
<table width="309" height="229" align="center">
<tr>
<td width="106">姓名:</td>
<td width="240"><input type="text" name="name"></td>
</tr>
<tr>
<td height="47">专业:</td>
<td><input type="text" name="type"></td>
</tr>
<tr>
<td height="48">照片:</td>
<td><input type="file" name="photo" ></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="添加">
<input type="reset" name="Submit2" value="重置"></td>
</tr>
</table>
</form>在Servlet中
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.commons.fileupload.*;public class photoServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private String uploadPath = "image\\doctor\\";
private String tempPath = "image\\doctor\\"; // 用于存放临时文件的目录
//Initialize global variables
public void init() throws ServletException {
} //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk"); } //Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
try {
if(!new File(uploadPath).isDirectory()){
new File(uploadPath).mkdirs();
}
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(4194304);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4096);
// 设置临时目录:
fu.setRepositoryPath(tempPath);
// 得到所有的文件:
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
// 获得文件名,这个文件名包括路径:
String fileName = fi.getName();
if (fileName != null) {
fi.write(new File(uploadPath
+ "\\" + fileName.substring(fileName.lastIndexOf("\\") + 1)));
String name = request.getParameter("name");
System.out.print(name);
}
}
response.sendRedirect("adddoctor.jsp");
} catch (Exception e) {
// 可以跳转出错页面
}
} //Clean up resources
public void destroy() {
}
}同时我要把jsp中的信息写如数据库 应该怎么样实现哪位高手是否可以详细讲解
解决方案 »
- 用jquery ajax实现 文件下载功能。是不是要使用插件才行啊。
- Eclipse使用link加载Aptana出现异常!!!
- jsp 字符串含有“%>”怎么转义 在线等。。。。。。。。
- JAVA端另存EXCEL文件的问题
- jsp如何解决通过该文件的扩展名能上传问题
- 程序运行时为什么又是会突然变得很慢
- 看来几个帖子说,“提交后重定向到...",如果是重定向到本页,那不是又执行了一遍啊,而且是反复执行
- 请大家看看我的这段代码,为什么当SQL选出来的纪录为空的时候就出错呢?
- web应用打war包部署在weblogic,启动报错Cannot set web app root system property..........
- 请教一个关于SESSION的问题!
- 求助大侠,关于sun.net.ftp的问题.
- spring 泛型配置
有啥不可的
jdbc 存呗
request获得这些字段 ......
以前的request里的数据已经都转移到它的解析类里了,你再查查api呗。