SPRINGMVC return file server address path after uploading files

Source: Internet
Author: User

Write a form first:

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>File Upload</title></Head><Body>    <H2>File Upload</H2>    <formAction= "/fileupload.jspx"enctype= "Multipart/form-data"Method= "POST">        <Table>            <TR>                <TD>File Description:</TD>                <TD><inputtype= "text"name= "description"></TD>            </TR>            <TR>                <TD>Please select a file:</TD>                <TD><inputtype= "File"name= "uploadfile"></TD>            </TR>            <TR>                <TD><inputtype= "Submit"value= "Upload"></TD>            </TR>        </Table>    </form></Body></HTML>

The action layer receives file processing:

/*** Resource file Upload, return a file server address *@authorLibin * @date January 12, 2018 morning 10:28:08*/@RequestMapping (Value= "/fileupload.jspx")     Public voiduploadfilebackaddress (httpservletrequest request, httpservletresponse response) {Try{multipartfile Multipartfile=NULL; if(Requestinstanceofmultiparthttpservletrequest) {Multipartfile= ((multiparthttpservletrequest) request). GetFile ("UploadFile"); }            if(NULL!=multipartfile) {                /*** Project Server address path*/String Projectserverpath= Request.getscheme () + "://" +request.getservername () + ":" +Request.getserverport ()+ Request.getcontextpath () + "/upload/"; /*** Upload file absolute path*/String Path= Request.getsession (). Getservletcontext (). Getrealpath ("/web-inf/upload/"); /*** Upload file name*/String FileName=Makefilename (Multipartfile.getoriginalfilename ()); File File=NewFile (path +fileName); /*** Determine if the path exists, and if it does not exist, create a*/                if(!File.getparentfile (). exists ())                {File.getparentfile (). Mkdirs (); }                /*** Create a file*/Multipartfile.transferto (NewFile (path + file.separator +fileName)); /*** Return server file address*/String Serverfilepatn= Projectserverpath +FileName;             }                    }  Catch(Exception e) {ajaxbackdata= This. Getajaxbackdataexceptionstatus (e); }           }  
SPRINGMVC configuration file:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jee= "Http://www.springframework.org/schema/jee"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-4.0.xsd Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/ Spring-jee-4.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc.xsd "Default-lazy-init= "true">    <Context:annotation-config/>    <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver">        < Propertyname= "Defaultencoding"value= "Utf-8"></ Property>        < Propertyname= "Maxuploadsize"value= "10485760000"></ Property>        < Propertyname= "Maxinmemorysize"value= "40960"></ Property>    </Bean>    <Mvc:annotation-driven></Mvc:annotation-driven>    <!--the Web. XML configuration Detectallviewresolvers is false not based on the Viewresolver interface full scan, only find Dispatcherservlet view resolver based on Viewresolver identity -    <BeanID= "Defaultviewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "Viewclass"value= "Org.springframework.web.servlet.view.JstlView" />        <!--set the prefix, which is the path to which the view is located -        < Propertyname= "prefix"value= "/web-inf/jsp/" />        <!--set the suffix, which is the extension of the view -        < Propertyname= "suffix"value= ". jsp" />    </Bean>   </Beans>



Reference article: http://www.cnblogs.com/klslb/p/8286762.html

jar Package to add: Commons-fileupload-1.2.1.jar Commons-io.2.5.jar

Springmvc File server address path after 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.