网站地图    收藏   

主页 > 前端 > css教程 >

文件上传 - html/css语言栏目:html.css - 自学php

来源:自学PHP网    时间:2015-04-14 14:50 作者: 阅读:

[导读] [html]%@ taglib prefix=s uri=/struts-tags%html head title文件上传/title /head body s:form name=idFrmMain method=post enctype=multipart/form-data table style......

[html
<%@ taglib prefix="s" uri="/struts-tags"%> 
<html> 
    <head> 
        <title>文件上传</title> 
    </head> 
    <body> 
        <s:form name="idFrmMain" method="post" enctype="multipart/form-data"> 
            <table style="margin-left: 100px;" border=0 cellspacing=0 cellpadding=2 bordercolor="#FFFFFF" width="400"> 
                <col span=1 width="3600"> 
                <tr> 
                    <th>上传文件</th> 
                    <td> 
                        <s:file name="file" size="40" onkeypress="return false;" /> 
                    </td> 
                </tr> 
                <tr align="center"> 
                    <td> 
                        <input type="button" style="width: 80px; height: 25" value="上传" onclick="javascript:upload();"> 
                    </td> 
                </tr> 
            </table> 
        </s:form> 
    </body> 
</html> 
 
文件为file,文件名必须为file+FileName(即fileFileName),只有这样才能通过get和set方法自动获取到文件名。该文件名不包括文件的路径
表单中必须加enctype="multipart/form-data",它用来设置表单的MIME编码。默认情况,编码格式是application/x-www-form-urlencoded,不能用于文件上传;
只有使用了multipart/form-data,才能完整的传递文件数据.enctype="multipart/form-data"是上传二进制数据。
[java] 
/** 文件 */ 
private File file; 
 
/** 文件名 */ 
private String fileFileName; 
 
public File getFile() { 
    return file; 

 
public void setFile(File file) { 
    this.file = file; 

 
public String getFileFileName() { 
    return fileFileName; 

 
public void setFileFileName(String fileFileName) { 
    this.fileFileName = fileFileName; 

 
String fileName = this.getFileFileName(); 
File fileObj = this.getFile(); 
 
String uploadPath = ServletActionContext.getServletContext().getRealPath("upload");//上传服务器路径 
uploadFile(uploadPath, fileName, FileIO.read(fileObj));//调用uploadFile()方法 

[java]
public static void uploadFile(String path, String fileName, 
    StringBuffer buffer) throws Exception { 
 
    File file = new File(path); 
    if (!file.exists()) { 
        file.mkdirs(); 
    } 
 
    String fullFileName = path + File.separatorChar + fileName; 
    BufferedWriter bw = null; 
    try { 
        bw = getBufferedWriter(fullFileName, false); 
        bw.write(buffer.toString()); 
        bw.flush(); 
    } catch (FileNotFoundException e) { 
        throw new FwApplicationException("文件不存在"); 
    } finally { 
        if (bw != null) { 
            bw.close(); 
        } 
    } 

[java] 
public static BufferedWriter getBufferedWriter(String fileName, 
    boolean writeMode) throws Exception { 
 
    FileOutputStream fos = null; 
    OutputStreamWriter osw = null; 
    try { 
        fos = new FileOutputStream(fileName, writeMode); 
        osw = new OutputStreamWriter(fos, "ISO8859-1"); 
        return new BufferedWriter(osw); 
    } catch (Exception e) { 
        try { 
            if (osw != null) { 
                osw.close(); 
            } else if (fos != null) { 
                fos.close(); 
            } 
        } catch (IOException ioException) { 
            LOG.debug("关闭时发生异常", ioException); 
        } 
        throw e; 
    } 

调用js里的upload()函数
[javascript] 
function upload() { 
    document.idFrmMain.action = "fileUpload.do"; 
    document.idFrmMain.target = "_self"; 
    document.idFrmMain.submit(); 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论