Java code for Image Upload

Source: Internet
Author: User

Private file; private string filefilename; private string picture; // getter and setter inputstream is = new fileinputstream (File ); // import the input stream of an IO stream string root = servletactioncontext. getrequest (). getrealpath ("/bookpicture"); // obtain the relative address through the request, and add/bookpicturefile F = new file (root, this. getfilefilename (); // defines a file. The first parameter is the file path, and the second parameter is the file name picture = ". "+" \ "+" bookpicture "+" \ "+ this. getfilefilename (); // assign a value to the picture string, // address/file name system. out. println ("======= picture ====" + picture); // output pictureoutputstream OS = new fileoutputstream (f) from the console ); // The output stream byte [] buffer = new byte [1024] of the first file; // defines a bufer string with a length of 1024 int Len = 0; while (LEN = is. read (buffer)> 0) {// if the information read from the specified file ends, the system continues to loop. write (buffer, 0, Len); // write the content read from the file to the specified file}

Package com; import Java. io. *; import javax. servlet. *; import javax. servlet. HTTP. *; import javax. servlet. JSP. *; import COM. jspsmart. upload. *; public class uploadfiles extends httpservlet {public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {// uses a third-party component, stored in response under Web-INF/lib. setcontenttype ("text/html; charset = gb2312"); // pagecontext is required for the smartupload initialization method, therefore, to get pageconext in servlet, you must first obtain the jspfactory instance. // use the getpagecontext method of the jspfactory instance to obtain the jspfactory JF = NULL instance of pageconext; // obtain jspfactory instance JF = jspfactory. getdefafactory Factory ();/* getpagecontext (servlet, servletrequest request, servletresponse response, Java. lang. string errorpageurl, Boolean needssession, int buffer, Boolean autoflush) */pagecontext = JF. getpagecontext (this, request, response, null, true, 8192, true); try {// instantiate smartuploadsmartupload mysmartupload = new smartupload (); // initialize the smartupload instance, mysmartupload. initialize (pagecontext); // sets the maximum number of bytes to be uploaded. In fact, this parameter can be left unspecified, indicating that the size of the uploaded file is not limited. // mysmartupload. settotalmaxfilesize (10000000); mysmartupload. upload (); // The following is a single file upload // the uploaded file is com. jspsmart. upload. file indicates that if the file name is repeated, it overwrites COM. jspsmart. upload. file file = mysmartupload. getfiles (). getFile (0); string uploadfilename = file. getfilename (); // call COM. jspsmart. upload. the saveas method of the file instance to save the file. At this time, the file name is // the file name saved to the server. saveas ("/upload/" + uploadfilename); Request Req = text T = .....; t. setupload (uploadfilename); T. set ..... (req);} catch (smartuploadexception e) {system. out. println (E. getmessage () ;}} protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, Java. io. ioexception {doget (request, response );}}

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.