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