CS 520 Web Programming File Upload and Email
CS 520 Web Programming File Upload and Email Chengyu Sun California State University, Los Angeles
File Upload – The Form <form action="File. Upload. Handler" method="post" enctype="multipart/form-data"> First file: <input type="file" name="file 1" /> Second file: <input type="file" name="file 2" /> <input type="submit" name="upload" value="Upload" /> </form>
File Upload – The Request POST / HTTP/1. 1 Host: cs. calstatela. edu: 4040 […] Cookie: SITESERVER=ID=289 f 7 e 73912343 a 2 d 7 d 1 e 6 e 44 f 931195 Content-Type: multipart/form-data; boundary=--------------146043902153 Content-Length: 509 ---------------146043902153 Content-Disposition: form-data; name="file 1"; filename="test. txt" Content-Type: text/plain this is a test file. ---------------146043902153 Content-Disposition: form-data; name="file 2"; filename="test 2. txt. gz" Content-Type: application/x-gzip �? ? ? ��? ? ? UC
Apache commons-fileupload http: //jakarta. apache. org/commons/fileuploa d/using. html File. Item. Factory file. Item. Factory = Disk. File. Item. Factory( 8096, "/tmp" ); Servlet. File. Upload file. Upload = new Servlet. File. Upload( file. Item. Factory ); List items = file. Upload. parse. Request( request ); for( Object o : items ) { File. Item item = (File. Item) items; if( ! item. is. Form. Filed() ) {. . . } }
Spring File Upload Support multipart. Resolver bean n Support multiple request parser libraries Handle uploaded files n Treat a uploaded file as a String or byte[] field w Spring Reference Documentation, Chapter 13. 8 http: //static. springframework. org/spring/docs/2. 0. x/refer ence/mvc. html n Use Multipart. Http. Servlet. Request and Multipart. File w http: //static. springframework. org/spring/docs/2. 0. x/api/o rg/springframework/web/multipart/Multipart. Http. Servlet. R equest. html
Spring File Upload Example CSNS: student/Upload. Files. Controller. java if( request instanceof Multipart. Http. Servlet. Request ) { Iterator i = r. get. File. Names(); if( i. has. Next() ) { Multipart. File uploaded. File = r. get. File( (String) i. next() ); if( !uploaded. File. is. Empty() ) {. . . } } }
Saving the Uploaded Files Database n n Option A: BINARY VARCAR or VARCHAR Option B: BLOB or CLOB Option C: disk file Pros and Cons? ?
How Email Works SMTP, IMAP, POP Email Server A Email Server B ? ? Client A ? ? Client B
Java. Mail http: //java. sun. com/products/javamail/ Properties props = System. get. Properties(); props. put("mail. smtp. host", mailhost); Session session = Session. get. Instance( props ); Message msg = new Mime. Message(session); . . . Transport. send( msg );
Spring Email Support Declare a mail. Sender bean Mail message classes n Simple. Mail. Message w No attachment, no special character encoding n Mime. Mail. Message
Configure Mail Sender <bean id="mail. Sender" class="org. springframework. mail. javamail. Java. Mail. Sender. Impl"> <property name="host“ value=“localhost”> </bean> Additional properties n n port username, password
Simple. Mail. Message Example CSNS: Reset. Password. Controller. java Simple. Mail. Message message; message = new Simple. Mail. Message( message. Template ); message. set. To( user. get. Email() ); message. set. Text( "Your password has been reset to. . . " ); mail. Sender. send( message );
Lessons Learned It’s simple as long as you understand some basics and find the right tools/libraries.
- Slides: 13