File Upload Tool class Uploadutil.java

Source: Internet
Author: User

  1. Package com.util;
  2. Import Java.io.BufferedInputStream;
  3. Import Java.io.BufferedOutputStream;
  4. Import Java.io.File;
  5. Import Java.io.FileInputStream;
  6. Import Java.io.FileOutputStream;
  7. Import Java.io.InputStream;
  8. Import Java.io.OutputStream;
  9. Import Java.util.Calendar;
  10. /**
  11. * File Upload Tool class
  12. *
  13. */
  14. Public class Uploadutil {
  15. private static final int buffer_size = 1024;
  16. //Save picture
  17. public static synchronized void copy (file src, file newFile) {
  18. try {
  19. InputStream is = null;
  20. OutputStream OS = null;
  21. try {
  22. is = new Bufferedinputstream (new FileInputStream (SRC),
  23. Buffer_size);
  24. OS = new Bufferedoutputstream (new FileOutputStream (newFile),
  25. Buffer_size);
  26. byte[] buffer = new byte[buffer_size];
  27. While (is.read (buffer) > 0) {
  28. Os.write (buffer);
  29. }
  30. } finally {
  31. if (null! = is) {
  32. Is.close ();
  33. }
  34. if (null! = OS) {
  35. Os.close ();
  36. }
  37. }
  38. } catch (Exception e) {
  39. E.printstacktrace ();
  40. }
  41. }
  42. /** 
  43. * Return era name + month + days + hours + minutes + seconds + Random code
  44. * @return
  45. */
  46. @SuppressWarnings ("static-access")
  47. public static synchronized String GetTime () {
  48. Calendar calendar = Calendar.getinstance ();
  49. String year = calendar.get (calendar.  Year) + "";
  50. String month = (calendar.get (calendar.  MONTH) + 1) + "";
  51. String day = calendar.get (calendar.  Day_of_month) + "";
  52. String hour = calendar.get (calendar.  Hour_of_day) + "";
  53. String minute = calendar.get (calendar.  MINUTE) + "";
  54. String second = calendar.get (calendar.  SECOND) + "";
  55. String millisecond = Calendar.get (calendar.millisecond) + "";
  56. int r = (int) (Math.random () *100000);
  57. String random = string.valueof (r);
  58. return year + month + day + hour + minute + second + millisecond + random+"a";
  59. }
  60. }

File Upload Tool class Uploadutil.java

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.