java组件fileupload文件上传demo

 更新时间:2016年10月14日 11:53:50   作者:壹龙  
这篇文章主要为大家详细介绍了java组件fileupload文件上传demo ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在我们的web开发中,很多的时候都需要把本机的一些文件上传到web服务器上面去。

如:一个BBS系统,当用户使用这是系统的时候,能把本机的一些图片,文档上传到服务器上面去。然后其他用户可以去下载这些文件,那么这样的话,我们可以自己编程实现文件的上传,但是更好的方式是使用一些已有的组件帮助我们实现这种上传功能。

常用的上传组件:  

    Apache 的 Commons FileUpload

    JavaZoom的UploadBean

    jspSmartUpload

FileUpload下载地址

  http://commons.apache.org/fileupload/

  下载:commons-fileupload-1.2.2-bin.zip    得到:commons-fileupload-1.2.2.jar

  http://commons.apache.org/io/

  下载:commons-io-1.4-bin.zip       得到:commons-io-1.4.jar

upload.jsp

代码;

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>using commons Upload to upload file </title>
</head>
<style>
* { font-family: "宋体"; font-size: 14px }
</style>
<body>
<p align="center"> 请您选择需要上传的文件</p>
<form id="form1" name="form1" method="post" action="servlet/fileServlet" enctype="multipart/form-data">
 <table border="0" align="center">
 <tr>
 <td>上传人:</td>
 <td>
 <input name="name" type="text" id="name" size="20" ></td>
 </tr> 
 <tr>
 <td>上传文件:</td>
 <td><input name="file" type="file" size="20" ></td>
 </tr> 
 <tr> 
 <td></td><td>
 <input type="submit" name="submit" value="提交" >
 <input type="reset" name="reset" value="重置" >
 </td>
 </tr>
 </table>
</form>
</body>
</html>

FileUploadServlet.java代码:

package com.b510.example;

import java.io.File;
import java.io.IOException;
import java.util.*;


import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * 
 * @author XHW
 * 
 * @date 2011-7-26
 * 
 */
public class FileUploadServlet extends HttpServlet {

 private static final long serialVersionUID = -7744625344830285257L;
 private ServletContext sc;
 private String savePath;

 public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 doPost(request, response);
 }
 

 public void init(ServletConfig config) {
 // 在web.xml中设置的一个初始化参数
 savePath = config.getInitParameter("savePath");
 sc = config.getServletContext();
 }
 
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 request.setCharacterEncoding("UTF-8");
 DiskFileItemFactory factory = new DiskFileItemFactory();
 ServletFileUpload upload = new ServletFileUpload(factory);
 try {
 List items = upload.parseRequest(request);
 Iterator itr = items.iterator();
 while (itr.hasNext()) {
 FileItem item = (FileItem) itr.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());
  // item.getName()返回上传文件在客户端的完整路径名称
  System.out.println("上传文件的名称:" + item.getName());

  File tempFile = new File(item.getName());

  //上传文件的保存路径
  File file = new File(sc.getRealPath("/") + savePath, tempFile.getName());
  item.write(file);
  request.setAttribute("upload.message", "上传文件成功!");
  }else{
  request.setAttribute("upload.message", "没有选择上传文件!");
  }
 }
 }
 }catch(FileUploadException e){
 e.printStackTrace();
 } catch (Exception e) {
 e.printStackTrace();
 request.setAttribute("upload.message", "上传文件失败!");
 }
 request.getRequestDispatcher("/uploadResult.jsp").forward(request, response);
 }
}

uploadResult.jsp代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 
 <title>uploadResult</title>
 
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"> 
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

 </head>
 
 <body>
 ${requestScope['upload.message'] }
 <a href="/upload/uploadFile.jsp">上传文件</a>
 </body>
</html>

web.xml

代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <servlet>
 <description>This is the description of my J2EE component</description>
 <display-name>This is the display name of my J2EE component</display-name>
 <servlet-name>FileUploadServlet</servlet-name>
 <servlet-class>com.b510.example.FileUploadServlet</servlet-class>

  <!--设置初始化参数-->
 <init-param>
  <param-name>savePath</param-name>
  <param-value>uploads</param-value>
 </init-param>
 </servlet>

 <servlet-mapping>
 <servlet-name>FileUploadServlet</servlet-name>
 <url-pattern>/servlet/fileServlet</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 <welcome-file>uploadFile.jsp</welcome-file>
 </welcome-file-list>
</web-app>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • RocketMQ存储文件的实现

    RocketMQ存储文件的实现

    这篇文章主要介绍了RocketMQ存储文件的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 如何用idea数据库编写快递e站

    如何用idea数据库编写快递e站

    这篇文章主要介绍了如何用idea数据库编写快递e站,本文通过图文实例相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • Spring Boot 工程的创建和运行(图文)

    Spring Boot 工程的创建和运行(图文)

    这篇文章主要介绍了Spring Boot 工程的创建和运行(图文),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • SpringBoot LocalDateTime格式转换方案详解(前端入参)

    SpringBoot LocalDateTime格式转换方案详解(前端入参)

    这篇文章主要介绍了SpringBoot LocalDateTime格式转换(前端入参),本文用示例介绍SpringBoot全局格式配置,将前端传过来的时间自动转化为LocalDateTime,需要的朋友可以参考下
    2023-04-04
  • java中 利用正则表达式提取( )内内容

    java中 利用正则表达式提取( )内内容

    本篇文章,小编为大家介绍关于java中 利用正则表达式提取( )内内容,有需要的朋友可以参考一下
    2013-04-04
  • Struts2实现自定义拦截器的三种方式详解

    Struts2实现自定义拦截器的三种方式详解

    这篇文章主要介绍了Struts2实现自定义拦截器的三种方式详解,一些与系统逻辑相关的通用功能如权限的控制和用户登录控制等,需要通过自定义拦截器实现,本节将详细讲解如何自定义拦截器,需要的朋友可以参考下
    2023-07-07
  • WebService教程详解(二)

    WebService教程详解(二)

    这篇文章主要介绍了WebService教程详解(二) 的相关资料,需要的朋友可以参考下
    2016-03-03
  • Mybatis多参数及实体对象传递实例讲解

    Mybatis多参数及实体对象传递实例讲解

    在使用Mybatis的时候,经常会有各种各样的参数传递,不同类型,不同个数的参数,下面小编通过例子给大家讲解下Mybatis多参数及实体对象传递,一起看看吧
    2016-12-12
  • springBoot中的properties配置解析

    springBoot中的properties配置解析

    这篇文章主要介绍了springBoot中的properties配置解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Intellij IDEA 2017.3使用Lombok及常用注解介绍

    Intellij IDEA 2017.3使用Lombok及常用注解介绍

    这篇文章主要介绍了Intellij IDEA 2017.3使用Lombok及常用注解介绍,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09

最新评论