Java实现文件上传至服务器的方法
程序员文章站
2023-12-17 13:11:58
在我们的web开发中,很多的时候都需要把本机的一些文件上传到web服务器上面去。
如:一个bbs系统,当用户使用这是系统的时候,能把本机的一些图片,文档上传到服务器上...
在我们的web开发中,很多的时候都需要把本机的一些文件上传到web服务器上面去。
如:一个bbs系统,当用户使用这是系统的时候,能把本机的一些图片,文档上传到服务器上面去。然后其他用户可以去下载这些文件,那么这样的话,我们可以自己编程实现文件的上传
但是更好的方式是使用一些已有的组件帮助我们实现这种上传功能。
常用的上传组件:
apache 的 commons fileupload
javazoom的uploadbean
jspsmartupload
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" rel="external nofollow" > --> </head> <body> ${requestscope['upload.message'] } <a href="/upload/uploadfile.jsp" rel="external nofollow" >上传文件</a> </body> </html>
web.xml
<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>
本文已被整理到了《java上传操作技巧汇总》,欢迎大家学习阅读。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。