熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

演示Struts2實現簡單上傳代碼

2022-06-13   來源: Java開源技術 

  webxml  <?xml version= encoding=UTF?>
<webapp version=
    xmlns=
    xmlns:xsi=instance
    xsi:schemaLocation=
    app__xsd>
  <filter>
        <filtername>struts</filtername>
        <filterclass>orgapachestrutsdispatcherFilterDispatcher</filterclass>
    </filter>

  <filtermapping>
        <filtername>struts</filtername>
        <urlpattern>/*</urlpattern>
    </filtermapping>
</webapp>

  strutsxml

<?xml version= encoding=UTF ?>
<!DOCTYPE struts PUBLIC
    //Apache Software Foundation//DTD Struts Configuration //EN
    dtd>

  <struts>
    <package name=struts extends=strutsdefault>
        <action name=upload class=comxiestrutsuploadUploadAction>
            <result name=success>/upload/resultjsp</result>
        </action>
    </package>
</struts>

  uploadjsp

 

  <%@ page language=java contentType=text/html; charset=GB
    pageEncoding=GB%>
<%@ taglib prefix=s uri=/strutstags %>
<html>
<head>
<title>upload</title>
</head>
<body>
<s:form action=upload enctype=multipart/formdata>
    <s:textfield name=username id=username label=username/>
    <s:file name=file id=file label=file/>
    <s:submit/>
</s:form>
</body>
</html>
 resultjsp

  <%@ page language=java contentType=text/html; charset=GB
    pageEncoding=GB%>
<%@ taglib prefix=s uri=/strutstags%>
<html>
    <head>
        <title>result</title>
    </head>
    <body>
        <s:property value=username />
        <br>
        <s:property value=fileFileName />
    </body>
</html>
UploadActionjava

  package comxiestrutsupload;

  import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioInputStream;
import javaioOutputStream;

  import orgapachestrutsServletActionContext;

  import comopensymphonyxworkActionSupport;

  public class UploadAction extends ActionSupport {
    private String username;

  private File file;

  private String fileFileName; // 有屬性file+Filename固定組成

  private String fileContentType; // 有屬性file+ContentType固定組成

  public String getUsername() {
        return username;
    }

  public void setUsername(String username) {
        thisusername = username;
    }

  public File getFile() {
        return file;
    }

  public void setFile(File file) {
        thisfile = file;
    }

  public String getFileFileName() {
        return fileFileName;
    }

  public void setFileFileName(String fileFileName) {
        thisfileFileName = fileFileName;
    }

  public String getFileContentType() {
        return fileContentType;
    }

  public void setFileContentType(String fileContentType) {
        thisfileContentType = fileContentType;
    }

  @Override
    public String execute() throws Exception {
        InputStream is = new FileInputStream(file);
        String root = ServletActionContextgetRequest()getRealPath(/temp);
        File destFile = new File(root thisgetFileFileName());
        OutputStream os = new FileOutputStream(destFile);
        byte[] buffer = new byte[];
        int length = ;
        while ((length isread(buffer)) > ) {
            oswrite(buffer length);
        }
        isclose();
        osclose();
        return SUCCESS;
    }
}


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28582.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.