文件上传

当前位置 : 首页 > 网页制作 > CSS > 文件上传

文件上传

来源: 作者: 时间:2016-02-19 09:07
[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...

[] 
<%@ 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(); 

Tag:
网友评论

<