java组件fileupload文件上传demo

网友投稿 230 2023-07-03


java组件fileupload文件上传demo

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

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

常用的上传组件:

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

代码;WnmrxU

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

请您选择需要上传的文件

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");

Diskhttp://FileItemFactory 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"%>

${requestScope['upload.message'] }

上传文件

web.xml

代码:

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://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">

This is the description of my J2EE component

This is the display name of my J2EE component

FileUploadServlet

com.b510.example.FileUploadServlet

  <!--设置初始化参数-->

savePath

uploads

FileUploadServlet

/servlet/fileServlet

index.jsp

uploadFile.jsp

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://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">

This is the description of my J2EE component

This is the display name of my J2EE component

FileUploadServlet

com.b510.example.FileUploadServlet

  <!--设置初始化参数-->

savePath

uploads

FileUploadServlet

/servlet/fileServlet

index.jsp

uploadFile.jsp


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:浅析Java.IO输入输出流 过滤流 buffer流和data流
下一篇:java String 转成Double二维数组的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~