Javaweb SSH framework to upload files, such as 2007 of Excel

Source: Internet
Author: User

The following code is the code to upload Excel, in fact, is to upload files to the server, the code is similar, just the type of the received files can be changed. I use the server is TOMCAT7, so the uploaded file saved in D:\MyWork2014\.metadata\.me_tcat7\webapps\Graduate\uploadExcel1.jsp is a struts2 tag.importexcel.jsp
<%@ page language="Java"import="java.util.*"pageencoding="UTF-8"%><%@ taglib prefix="s"Uri="/struts-tags"%><! DOCTYPE HTML Public"-//w3c//dtd HTML 4.01 transitional//en">"stylesheet"Type="Text/css"href="Styles.css"> "Uploadexcel"Name="Importform"Enctype="Multipart/form-data"METHOD ="Post"> <div><s:file name="Upload"></s:file> <span><input type="Submit"Value="Confirm Upload"style="background-color: #bbbbbb; color:white;width:90px;height:30px"/> </span> </div> </s:form> </body>

2 Struts.xml

</action> <action name="Uploadexcel" class="uploadaction"Method="Uploadexcel"> <result name="uploadexcelsuccess">international.jsp</result> <result name="Error">importExcel.jsp</result> <param name="Allowedtypes">application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream</param> <!--//allow upload of file types, this is 2007EXCEL, xlsx suffix- -</action>

3.applicationcontext-action.xml

    <bean id="uploadaction"class="  Com.international.action.uploadExcelAction "scope="prototype">            </bean>

4. Uploadexcelaction.java

Package Com.international.action;import Java.io.file;import org.apache.commons.io.fileutils;import Org.apache.struts2.servletactioncontext;import Com.opensymphony.xwork2.ActionSupport; Public classUploadexcelaction extends actionsupport{PrivateFile upload;//used to catch the Excel file that the JSP passed over    PrivateString Uploadfilename;//This value does not have to be processed, you can get the file name of Excel you passed .    PrivateString Uploadcontenttype;//Uploadcontenttype This value is not processed, you can get the type of Excel file you passed .//such as: If it is 2007 Excel, it is Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet         PrivateString allowedtypes;//catch the value set in struts2 for file type validation     PrivateString Savepath;//set an absolute path for uploading Excel           Publicuploadexcelaction () {} PublicFile getupload () {returnupload; }     Public voidsetupload (File upload) { This. Upload =upload; }     PublicString Getuploadfilename () {returnUploadfilename; }     Public voidsetuploadfilename (String uploadfilename) { This. Uploadfilename =Uploadfilename; }     PublicString Getuploadcontenttype () {returnUploadcontenttype; }     Public voidSetuploadcontenttype (String uploadcontenttype) { This. Uploadcontenttype =Uploadcontenttype; }     PublicString getallowedtypes () {returnallowedtypes; }     Public voidsetallowedtypes (String allowedtypes) { This. Allowedtypes =allowedtypes; }     PublicString Getsavepath () {returnSavepath =Servletactioncontext.getservletcontext (). Getrealpath ("/uploadexcel"); }     Public voidSetsavepath (String savepath) { This. Savepath =Savepath; }         PublicString Uploadexcel () throws Exception {//Verifying file Formats//Boolean flag = false;//System.out.println ("uploadfilename:" + uploadfilename + "Uploadcontenttype:" + uploadcontenttype);//System.out.println ("Savepath:" + Getsavepath ());//string[] allowedtypesstr = Allowedtypes.split (",");//for (int i = 0; i < allowedtypesstr.length; i++) {//if (Uploadcontenttype.equals (Allowedtypesstr[i])) {//flag = true;//            }//        }//if (flag = = False) {//System.out.println ("Incorrect upload file format");//return "error";//        }        //Save FileFile Newexcel =NewFile (Getsavepath () +"\\"+uploadfilename); if(Newexcel.exists ()) {newexcel.delete (); }        Try{fileutils.copyfile (upload, newexcel); } Catch(Exception e) {e.printstacktrace (); }        //Delete temporary filesUpload.delete (); return "uploadexcelsuccess"; }          }

Javaweb SSH framework to upload files, such as 2007 of Excel

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.