Flex implements Multifile upload II: background (java)

Source: Internet
Author: User
Http://hi.baidu.com/half_cup_coffee/blog/item/d421001037f02307203f2e7c.html

In "one of Multiple File Uploads using Flex: front-end part", we introduced how to use flex to upload multiple files, so we have no time to put the subsequent files.

Here we will talk about how the background processes the uploaded files, using java.

The upload function uses the apache jar package. You can download it here.

The java code is as follows:

1. Set the upload Directory, which is the directory of the machine where the web server is located.
2. Prevent garbled settings
3. the upload function is written in action. In this example, the default method name doPost is used directly. If you want to use struts or other methods, you can define your own method name Java code.

  1. Package com. nstar. orderexpress. web;
  2. Import javax. servlet. http. HttpServlet;
  3. Import org. apache. log4j. Logger;
  4. Import javax. servlet. http. HttpServletRequest;
  5. Import javax. servlet. http. HttpServletResponse;
  6. Import javax. servlet. ServletException;
  7. Import org. apache. commons. fileupload. disk. DiskFileItemFactory;
  8. Import org. apache. commons. fileupload. servlet. ServletFileUpload;
  9. Import java. util. List;
  10. Import org. apache. commons. fileupload. FileUploadException;
  11. Import java. util. Iterator;
  12. Import org. apache. commons. fileupload. FileItem;
  13. Import java. io. File;
  14. Import java. io. UnsupportedEncodingException;
  15. Import org. apache. commons. lang. ObjectUtils;
  16. Public class FileUploadAction extends HttpServlet {
  17. Private static Logger logger = Logger. getLogger (FileUploadAction. class. getName ());
  18. Private int maxPostSize = 100*1024*1024;
  19. Private String uploadPath = "D :\\ upload \\";
  20. Public FileUploadAction (){
  21. }
  22. // File Upload
  23. Public void doPost (HttpServletRequest request, HttpServletResponse response)
  24. Throws ServletException {
  25. Logger ("begin upload ");
  26. Try {
  27. Request. setCharacterEncoding ("UTF-8"); // prevents garbled characters from being uploaded to the server
  28. } Catch (UnsupportedEncodingException e ){
  29. // TODO Auto-generated catch block
  30. E. printStackTrace ();
  31. }
  32. Response. setContentType ("text/html; charset = UTF-8 ");
  33. DiskFileItemFactory factory = new DiskFileItemFactory ();
  34. Factory. setSizeThreshold (1024*4 );
  35. ServletFileUpload upload = new ServletFileUpload (factory );
  36. Upload. setFileSizeMax (maxPostSize );
  37. Logger ("request ======================" + ObjectUtils. toString (request ));
  38. List fileItems = null;
  39. Try {
  40. FileItems = upload. parseRequest (request );
  41. Logger ("=============================" + ObjectUtils. toString (fileItems ));
  42. Iterator iter = fileItems. iterator ();
  43. While (iter. hasNext ()){
  44. FileItem item = (FileItem) iter. next ();
  45. Log (item. toString ());
  46. If (! Item. isFormField ()){
  47. String name = item. getName ();
  48. Logger ("file name =" + name );
  49. Try {
  50. Item. write (new File (uploadPath + name ));
  51. }
  52. Catch (Exception ex ){
  53. Logger (ex. getMessage ());
  54. }
  55. }
  56. }
  57. }
  58. Catch (FileUploadException ex1 ){
  59. Logger ("FileUploadException->" + ex1.getMessage ());
  60. }
  61. }
  62. Private void logger (String info ){
  63. System. out. println (info );
  64. }
  65. }
Package com. nstar. orderexpress. web; import javax. servlet. http. httpServlet; import org. apache. log4j. logger; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. servletException; import org. apache. commons. fileupload. disk. diskFileItemFactory; import org. apache. commons. fileupload. servlet. servletFileUpload; import java. util. list; import org. a Pache. commons. fileupload. fileUploadException; import java. util. iterator; import org. apache. commons. fileupload. fileItem; import java. io. file; import java. io. unsupportedEncodingException; import org. apache. commons. lang. objectUtils; public class FileUploadAction extends HttpServlet {private static Logger logger = Logger. getLogger (FileUploadAction. class. getName (); private int maxPostSize = 100*1024 * 1024; private String uploadPath = "D :\\ upload \"; public FileUploadAction () {}// File upload public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException {logger ("begin upload"); try {request. setCharacterEncoding ("UTF-8"); // prevent file names from being uploaded to server garbled characters} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch block e. printStackTrace ();} response. setConte NtType ("text/html; charset = UTF-8"); DiskFileItemFactory factory = new DiskFileItemFactory (); factory. setSizeThreshold (1024*4); ServletFileUpload upload = new ServletFileUpload (factory); upload. setFileSizeMax (maxPostSize); logger ("request ====================" + ObjectUtils. toString (request); List fileItems = null; try {fileItems = upload. parseRequest (request); logger ("==========================" + ObjectUtil S. toString (fileItems); Iterator iter = fileItems. iterator (); while (iter. hasNext () {FileItem item = (FileItem) iter. next (); log (item. toString (); if (! Item. isFormField () {String name = item. getName (); logger ("file name =" + name); try {item. write (new File (uploadPath + name);} catch (Exception ex) {logger (ex. getMessage () ;}}} catch (FileUploadException ex1) {logger ("FileUploadException->" + ex1.getMessage () ;}} private void logger (String info) {System. out. println (info );}}

There are basically so many file uploads. You can expand the functions you want on this basis.

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.