<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    龍行天下

      政 博
    隨筆 - 23, 文章 - 0, 評論 - 12, 引用 - 0
    數(shù)據(jù)加載中……

    用struts上傳文件

    用到的類import org.apache.struts.upload.FormFile;

    Jsp上傳頁面:

    ?

    <%@ page contentType="text/html; charset=GBK" %>

    <html>

    <head>

    <title>

    strutsUploadForm

    </title>

    </head>

    <body bgcolor="#ffffff">

    <h1>

    上傳測試

    </h1>

    <form action="uploadTestAction.do" method="post" enctype="multipart/form-data" name="form1">

    ? <p>文本

    ??? <input name="theText" type="text" id="theText">

    </p>

    ? <p>文件

    ??? <input name="theFile" type="file" id="theFile">

    </p>

    ? <p>保存到

    ??? <input name="saveTo" type="text" id="saveTo">

    </p>

    ? <p>

    ??? <input type="submit" name="Submit" value="提交">

    </p>

    </form>

    </body>

    </html>

    ?

    ?

    actionForm

    ?

    package hehe;

    ?

    import org.apache.struts.action.*;

    import javax.servlet.http.*;

    import org.apache.struts.upload.FormFile;

    ?

    public class UploadTestForm extends ActionForm {

    ? private String saveTo;

    ? private String theText;

    ? private org.apache.struts.upload.FormFile theFile;//文件框?qū)?yīng)的是formFile類型

    ? public String getSaveTo() {

    ??? return saveTo;

    ? }

    ? public void setSaveTo(String saveTo) {

    ??? this.saveTo = saveTo;

    ? }

    ? public org.apache.struts.upload.FormFile getTheFile() {

    ??? return theFile;

    ? }

    ? public void setTheFile(org.apache.struts.upload.FormFile theFile) {

    ??? this.theFile = theFile;

    ? }

    ? public String getTheText() {

    ??? return theText;

    ? }

    ? public void setTheText(String theText) {

    ??? this.theText = theText;

    ? }

    ? public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

    ??? /**@todo: finish this method, this is just the skeleton.*/

    ??? if(!this.getTheFile().getContentType().equals("image/pjpeg")){

    ????? System.out.println("不是jpg");

    ??? }//可以判斷類型

    ??? if(this.getTheFile().getFileSize()>100){

    ????? System.out.println("長度大于1000");

    ??? }//可以判斷大小

    ??? return null;

    ? }

    ? public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {

    ??? saveTo = null;

    ??? theFile = null;

    ??? theText = null;

    ? }

    }

    ?

    ?

    Action

    ?

    package hehe;

    ?

    import org.apache.struts.action.*;

    import org.apache.struts.upload.FormFile;

    import javax.servlet.http.*;

    import java.io.*;

    ?

    public class UploadTestAction

    ??? extends Action {

    ? public ActionForward execute(ActionMapping actionMapping,

    ?????????????????????????????? ActionForm actionForm,

    ?????????????????????????????? HttpServletRequest request,

    ?????????????????????????????? HttpServletResponse response) {

    ??? UploadTestForm uploadForm = (UploadTestForm) actionForm;

    ??? FormFile file = uploadForm.getTheFile();

    ??? String path = uploadForm.getSaveTo();

    ??? String theText = uploadForm.getTheText();

    ??? try {

    ????? InputStream input = file.getInputStream();//能從FormFile中獲得輸入流

    ????? OutputStream output = new FileOutputStream(path);

    ????? int bytesReader = 0;

    ????? byte[] readbuffer = new byte[8192];

    ????? while ( (bytesReader = input.read(readbuffer, 0, 8192)) != -1) {

    ??????? output.write(readbuffer, 0, bytesReader);

    ????? }

    ????? output.close();

    ??? }

    ??? catch (Exception e) {

    ????? e.printStackTrace();

    ??? }

    ??? request.setAttribute("theText", theText);

    ??? request.setAttribute("fileName", file.getFileName());//上傳的文件名

    ??? request.setAttribute("fileSize", new Integer(file.getFileSize()));//文件大小

    ??? request.setAttribute("fileType", file.getContentType());//文件類型

    ??? return actionMapping.findForward("success");

    ? }

    }

    ?

    ?

    結(jié)果頁面

    ?

    <%@ page contentType="text/html; charset=GBK" %>

    <html>

    <head>

    <title>

    strutsUploadResult

    </title>

    </head>

    <body bgcolor="#ffffff">

    <h1>

    上傳結(jié)果

    </h1>

    文件名:<%=request.getAttribute("fileName")%><br />

    文本:<%=request.getAttribute("theText")%><br />

    文件類型:<%=request.getAttribute("fileType")%><br />

    文件大小:<%=request.getAttribute("fileSize")%><br />

    </body>

    </html>

    posted on 2006-05-03 20:03 feingto 閱讀(1170) 評論(1)  編輯  收藏 所屬分類: Java Web

    評論

    # re: 用struts上傳文件  回復(fù)  更多評論   

    你好,你在那呀

    你在北京嗎
    你是姓康名鋒嗎?
    我在蘭州
    以后有好我問題得請教你呀

    你在萬維工作嗎?
    2006-08-06 10:31 | 天外流星
    主站蜘蛛池模板: 看免费毛片天天看| 中文字幕乱码系列免费| 亚洲国产精品无码第一区二区三区 | 在线观看亚洲网站| 最近在线2018视频免费观看| 24小时免费直播在线观看| 亚洲一区二区高清| 亚洲精品视频在线观看视频| 亚洲国产无线乱码在线观看| 无码国产精品一区二区免费16| 美女黄网站人色视频免费国产| 亚洲第一精品在线视频| 欧洲亚洲综合一区二区三区| 精品女同一区二区三区免费站| 亚洲码国产精品高潮在线| 久久久久亚洲精品无码网址色欲| 亚洲毛片免费观看| 久久夜色精品国产噜噜噜亚洲AV| 中国一级毛片视频免费看| 免费看国产曰批40分钟| 亚洲中文字幕无码日韩| 久久国产一片免费观看| 亚洲人成网亚洲欧洲无码久久| 国产午夜不卡AV免费| 亚洲视频国产精品| 日韩中文字幕精品免费一区| 亚洲高清无在码在线无弹窗| 91禁漫免费进入| 亚洲熟女乱色一区二区三区| 91在线老王精品免费播放| 国产精品成人免费观看| 亚洲视频一区二区| 最近更新免费中文字幕大全| 亚洲综合精品网站| a毛片免费观看完整| 亚洲最大在线视频| 无码国产精品久久一区免费| 久久久久亚洲AV无码去区首| 在线免费观看国产视频| 亚欧洲精品在线视频免费观看 | 水蜜桃视频在线观看免费播放高清|