当社のWeb開発では、多くの場合、マシンからWebサーバーにいくつかのファイルをアップロードする必要があります。
たとえば、BBSシステムでは、ユーザーがこのシステムを使用すると、マシンの写真やドキュメントをサーバーにアップロードできます。その後、他のユーザーはこれらのファイルをダウンロードできます。このようにして、ファイルを自分でアップロードすることをプログラムできますが、より良い方法は、既存のコンポーネントを使用して、このアップロード機能を実現するのに役立つことです。
一般的なアップロードコンポーネント:
ApacheによるCommons fileupload
Javazoomのuploadbean
jspsmartupload
fileuploadダウンロードアドレス:
http://commons.apache.org/fileupload/
ダウンロード:commons-fileupload-1.2.2.2-bin.zip get:commons-fileupload-1.2.2.jar
http://commons.apache.org/io/
ダウンロード:Commons-Io-1.4-Bin.Zip Get:Commons-Io-1.4.jar
upload.jsp
コード;
<%@ページ言語= "java" contentType = "text/html; charset = utf-8"%> <html> <head> <head> <title> commonsアップロードしてファイルをアップロードする</title> </head> <style>* {font-family: "安体"; font-size:14px} </style> <body> <p align = "center">アップロードするファイルを選択してください</p> <form id = "form1" form1 "form1" method = "action =" servlet/fileservlet "enctype =" multipart/form-data "> <table"> <td <td <td <td <td> <td <td <td>名前= "name" type = "text" id = "name" size = "20"> </td> </tr> <tr> <td> upload file:</td> <td> <input name "type =" file "size =" 20 "> </td> </tr> <tr> <td> </td> <input" ">" ">" ""> " type = "reset" name = "reset" value = "reset"> </td> </tr> </table> </form> </body> </html>fileuploadservlet.javaコード:
パッケージ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.servlet.servletexception; javax.servlet.http.httpservletrequest; import javax.servlet.http.httservletresponse; Import org.apache.commons.fileupload.fileitem; Import org.apache.commons.fileupload.fileuploadexception; 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;プライベート文字列SavePath; public void doget(httpservletrequest request、httpservletResponse応答)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応答)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( "formパラメーター名:" + item.getfieldname() + "、formパラメーター値:" + 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( "uploadファイル名:" + item.getName())にアップロードされたファイルのフルパス名を返します。 file tempfile = new file(item.getName()); //ファイルをアップロードしてくださいパスファイル= newファイル(sc.getRealPath( "/") + savepath、tempfile.getName()); item.write(file); request.setattribute( "upload.message"、 "ファイルのアップロードが成功しました!"); } else {request.setattribute( "upload.message"、 "uploadファイルを選択しない!"); }}}}} catch(fileuploadexception e){e.printstacktrace(); } catch(Exception e){e.printstacktrace(); request.setattribute( "upload.message"、 "file failed!"); } request.getRequestDispatcher( "/uploadResult.jsp")。 }}uploadResult.jspコード:
<%@ page Language = "Java" Import = "Java.util。*" PageEncoding = "UTF-8"%> <!doctype HTML public " - // w3c // dtd html 4.01 transitional // en" <Meta http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "content =" 0 "> <meta http-equiv =" keywords "content =" keyword1、keyword2、keyword3 "> <meta http-equiv =" type = "text/css" href = "styles.css"> - > </head> <body> $ {requestscope ['upload.message']} <a href = "/upload/uploadfile.jsp"> uploadファイル</a> </body> </html>web.xml
コード:
<?xml version = "1.0" encoding = "utf-8"?> <web-appバージョン= "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instcance" xsi:schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/ns/javaee/web-app_55.55.55.55.55.55.55.55.55.55.5.55.55.55.55.5.55.5.55.5.55.5.55.55.5.5.55.5.55.55.55.5.55.55.5.5.55.5.55.55.5.5.55.5.55.5.5.55.5.55.5.5.5.5.5.55.5.5.5.5.5.5.5.5.5.コンポーネント</description> <display-name>これは私のj2eeコンポーネントのディスプレイ名</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-mapping> <servlet-name> fileuploadservlet </servlet-name> <url-pittern> <welcome-file-list> <welcome-file> index.jsp </welcome-file> <welcome-file> uploadfile.jsp </welcome-file> </welcome-file-list> </web-app>
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。