Struts Learning Summary-04 uploading files

Source: Internet
Author: User
Tags i18n

1. upload.jsp

<%@ page language="Java"import="java.util.*"pageencoding="Utf-8"%><%@ taglib uri="/struts-tags"prefix="s"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () +"://"+request.getservername () +":"+request.getserverport () +path+"/";%><! DOCTYPE HTML Public"-//w3c//dtd HTML 4.01 transitional//en">"Myupload"Enctype="Multipart/form-data"> <s:file name="myfile"></s:file> <s:submit value="Submit"></s:submit> </s:form> </body>

2 uploadresult.jsp

<%@ page language="Java"import="java.util.*"pageencoding="GB18030"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () +"://"+request.getservername () +":"+request.getserverport () +path+"/";%><! DOCTYPE HTML >Upload Success!! </body>

3 Uploadaction.java

Package Com.xiping.action;import java.io.file;import java.io.fileinputstream;import java.io.FileOutputStream; Import Java.io.inputstream;import java.io.outputstream;import com.opensymphony.xwork2.ActionSupport; Public classUploadaction extends Actionsupport {PrivateFile myfile;//corresponds to a file that is actually uploaded    PrivateString Myfilefilename;//corresponds to the uploaded file name     PublicFile Getmyfile () {returnmyfile; }     Public voidsetmyfile (File myfile) { This. MyFile =myfile; }     PublicString Getmyfilefilename () {returnMyfilefilename; }     Public voidsetmyfilefilename (String myfilefilename) { This. Myfilefilename =Myfilefilename; } @Override PublicString Execute () throws Exception {InputStream is=NewFileInputStream (myfile); OutputStream OS=NewFileOutputStream ("e://temp3//"+myfilefilename); byte[] buffer =New byte[1024x768]; intLength =0;  while(-1! = (length = is. Read (buffer)) {os.write (buffer,0, length);        } os.close ();  is. Close (); returnSUCCESS; }}

4 Struts.xml

<?xml version="1.0"encoding="UTF-8"? ><! DOCTYPE Struts Public"-//apache software foundation//dtd Struts Configuration 2.3//en" "HTTP://STRUTS.APACHE.ORG/DTDS/STRUTS-2.3.DTD"><struts> <!--all requests to match *.action are handled by STRUTS2-<constant name="struts.action.extension"Value="Action"/> <!--Enable Development mode--<constant name="Struts.devmode"Value="true"/> <!--struts profile changes, reload-<constant name="Struts.configuration.xml.reload"Value="true"/> <!--Set whether the browser caches static content--<constant name="Struts.serve.static.browserCache"Value="false"/> <!--request parameters are encoded--<constant name="struts.i18n.encoding"Value="Utf-8"/> <!--Reload the resource file every time the HTTP request system helps develop-<constant name="Struts.i18n.reload"Value="true"/> <!--file Upload maximum-<constant name="struts.multipart.maxSize"Value="104857600"/> <!--Let struts2 support dynamic method calls--<constant name="struts.enable.DynamicMethodInvocation"Value="true"/> <!--The action name is still slash-and-<constant name="Struts.enable.SlashesInActionNames"Value="false"/> <!--allow expression syntax in tags--<constant name="Struts.tag.altSyntax"Value="true"/> <package name="Struts"extends="Struts-default"> <action name="Myupload" class="com.xiping.action.UploadAction"> <result name="Success">/uploadResult.jsp</result> </action> </package></struts>

Struts Learning Summary-04 uploading files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.