java实现文件上传
2010-03-25 14:55
302 查看
public class Upload extends HttpServlet {
private ServletContext sc;
private String savePath;
public Upload() {
super();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (item.isFormField()) {
System.out.println("表单的参数名称: " + item.getFieldName()
+ ",对应的参数值: " + item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {
System.out.println("上传的文件的大小: " + item.getSize());
System.out.println("上传的文件的类型: " + item.getContentType());
System.out.println("上传的文件的名称: " + item.getName());
File tempFile = new File(item.getName());
System.out.println(tempFile.getName());
File file = new File(sc.getRealPath("/") + savePath,tempFile.getName());
System.out.println(file);
item.write(file);
request.setAttribute("upload.message", "文件上传成功");
} else {
request.setAttribute("upload.message", "没有选择上传文件");
}
}
}
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("upload.message", "上传文件不成功! ");
}
request.getRequestDispatcher("uploadResult.jsp").forward(request,response);
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void init(ServletConfig config) throws ServletException {
savePath = "upload";
sc = config.getServletContext();
}
}
<form action="Up" method="post" enctype="multipart/form-data">
<table width="350" border="0" align="center">
<tr height="50">
<td colspan="2" align="center">文件上传</td>
</tr>
<tr align="center">
<td width="40%">上传文件</td>
<td><label>
<input type="file" name="uploadfile" id="uploadfile" />
</label></td>
</tr>
<tr align="center">
<td align="right"><label>
<input type="reset" name="button" id="button" value="重置" />
</label></td>
<td><label>
<input type="submit" name="button2" id="button2" value="提交" />
</label></td>
</tr>
</table>
</form>
private ServletContext sc;
private String savePath;
public Upload() {
super();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (item.isFormField()) {
System.out.println("表单的参数名称: " + item.getFieldName()
+ ",对应的参数值: " + item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {
System.out.println("上传的文件的大小: " + item.getSize());
System.out.println("上传的文件的类型: " + item.getContentType());
System.out.println("上传的文件的名称: " + item.getName());
File tempFile = new File(item.getName());
System.out.println(tempFile.getName());
File file = new File(sc.getRealPath("/") + savePath,tempFile.getName());
System.out.println(file);
item.write(file);
request.setAttribute("upload.message", "文件上传成功");
} else {
request.setAttribute("upload.message", "没有选择上传文件");
}
}
}
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("upload.message", "上传文件不成功! ");
}
request.getRequestDispatcher("uploadResult.jsp").forward(request,response);
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void init(ServletConfig config) throws ServletException {
savePath = "upload";
sc = config.getServletContext();
}
}
<form action="Up" method="post" enctype="multipart/form-data">
<table width="350" border="0" align="center">
<tr height="50">
<td colspan="2" align="center">文件上传</td>
</tr>
<tr align="center">
<td width="40%">上传文件</td>
<td><label>
<input type="file" name="uploadfile" id="uploadfile" />
</label></td>
</tr>
<tr align="center">
<td align="right"><label>
<input type="reset" name="button" id="button" value="重置" />
</label></td>
<td><label>
<input type="submit" name="button2" id="button2" value="提交" />
</label></td>
</tr>
</table>
</form>
相关文章推荐
- !JAVA学习提高之---- Struts实现多文件上传
- JAVA实现:将文件从本地上传到HDFS上、从HDFS上读取等操作
- Java实现多个文件上传(验证文件大小、文件类型)
- Java动态显示文件上传进度的简单实现 - java2000.net的专栏 - CSDNBlog
- Java网络编程实践和总结 --- 基于TCP的Socket编程之实现文件上传和下载服务
- HTTP POST请求报文格式分析与Java实现文件上传
- java实现文件的上传、下载和删除的后台代码
- java实现上传文件到服务器和客户端
- Java实现文件的上传 推荐
- 【java实现web文件无刷新上传】
- java zyUpload 实现多文件上传
- JAVA中使用FTPClient实现文件上传下载
- JAVA应用XFire框架来实现WebServie的大文件传输功能之二(上传)
- java实现异步上传文件
- maven工程 java 实现文件上传 SSM ajax异步请求上传
- java简单多线程方式+实现文件上传(spring mvc + jquery.form.js 框架)
- 详解Java的Struts框架中上传文件和客户端验证的实现
- Java使用内存映射实现大文件的上传
- java组件commons-fileupload实现文件上传、下载、在线打开