Java simple upload image example

Source: Internet
Author: User

Front-end page:
<! -- Note that enctype indicates the MIME encoding of the form. By default, the encoding format is application/x-www-form-urlencoded and cannot be used for file upload. Only multipart/form-data can be used to completely transmit file data, perform the following operations. -->
<Form id = "addform" method = "post" enctype = "multipart/form-data" action = "$ {base}/action/addPhoPic">
<Table border = "0" class = "perview" align = "center">
<A href = "#" onClick = "toaddpic ()"> upload to album </a>
<Tr>
<Th> select a file </th>
<Th width = "50%"> preview </th>
</Tr>
<Tr>
<Td height = "200"> <input id = "idFile" name = "upload" type = "file"/> </td>
<Td align = "center"> </td>
</Tr>
</Table>
</Form>
Java background processing:
// Consistent with the name of the front-end page
Private File upload;
// Reflection, get the file type and file name
Private String uploadContentType;
Private String uploadFileName;
Public String doAddPhoPic (){
// Process your own PhotoService Interface
IPhotoService photoService = BeanFactory. getBean (BeanConstants. WB_PHOTO_SERVICE );
Photo photo = new Photo ();
// Here, the simple demo does not have to specify the name specification or cut or contract the image. Therefore, the uploaded image is simply named 1, 2, and 3.
Int count = photoService. queryPhotoList (). size ();
Count ++;
String file_path1 = "";
String file_path2 = "";
Try {
// Upload to the local directory of the project
File_path1 = Constants. BASE_ROOT + "/fullsize" + "/" + count + ". jpg ";
File_path2 = Constants. BASE_ROOT + "/thumbs" + "/" + count + ". jpg ";
Photo. setPicName (photoService. queryPhotoList (). size () + 1 + ". jpg ");
Photo. setPicUrl (file_path2 );
PhotoService. insertPhoto (photo );
System. out. println ("---" + file_path1 );
System. out. println ("---" + file_path2 );
// Write the object
FileOutputStream fos1 = new FileOutputStream (file_path1 );
FileOutputStream fos2 = new FileOutputStream (file_path2 );
// Read the object
FileInputStream FCM = new FileInputStream (upload );
Byte [] buffer = new byte [1024];
Int len = 0;
// Read the stream and save it to the byte array
While (len = FS. read (buffer)> 0 ){
Fos1.write (buffer, 0, len );
Fos2.write (buffer, 0, len );
}
Fos1.close ();
Fos2.close ();
FCM. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
List = photoService. queryPhotoList ();
Return SUCCESS;
}
 
This article is from the "Youth bag" blog

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.