Archive | Java
get session into action class in struts 2.x
JSPs contain implicit session object to use in the jsp pages, when an action in invoked in struts we need to get ‘session ‘ into the action class, we can do this by implementing “SessionAware” interface. Eclipse will ask you to implement an unimplemented method click on that meesage and eclipse will create the method for you called “public void setSession(Map<String, Object> arg0){//TODO}”
Uploading video to youtube using aouth authentication in java
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.media.MediaStreamSource;
import com.google.gdata.data.media.mediarss.MediaCategory;
import com.google.gdata.data.media.mediarss.MediaDescription;
import com.google.gdata.data.media.mediarss.MediaKeywords;
import com.google.gdata.data.media.mediarss.MediaTitle;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.google.gdata.data.youtube.YouTubeNamespace;
import com.google.gdata.util.ServiceException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class YouTubeUploadService
{
public static final String YOUTUBE_GDATA_SERVER = "http://gdata.youtube.com";
public static final String VIDEOS_FEED = YOUTUBE_GDATA_SERVER
+ "/feeds/api/videos";
public static final String USER_FEED_PREFIX = YOUTUBE_GDATA_SERVER
+ "/feeds/api/users/";
public static final String ACTIVITY_FEED_PREFIX = YOUTUBE_GDATA_SERVER
+ "/feeds/api/events";
public static final String UPLOADS_FEED_SUFFIX = "/uploads";
public static final String FAVORITES_FEED_SUFFIX = "/favorites";
public static final String PLAYLISTS_FEED_SUFFIX = "/playlists";
public static final String FRIENDS_ACTIVITY_FEED_SUFFIX = "/friendsactivity";
public static final String DEFAULT_USER = "default";
public static final String DEFAULT_VIDEO_ID = "scoMN8DYkCw";
public static final String VIDEO_UPLOAD_FEED =
"http://uploads.gdata.youtube.com/feeds/api/users/"
+ DEFAULT_USER + "/uploads";
/**
* Uploads a new video to YouTube.
*/
public void uploadVideoWithFileSource(String userid,String developerKey,String authtoken,String path,String mimetype,String title) throws IOException
{
File videoFile = new File(path);
if (!videoFile.exists()) {
logger.info("Sorry, that video doesn't exist.");
response.setError("Sorry, that video doesn't exist.");
return response;
}
return uploadVideoWithInputStream(userid, developerKey, authtoken,new FileInputStream(videoFile) , mimetype, title);
}
public void uploadVideoWithByteData(String userid,String developerKey,String authtoken,byte[] data,String mimetype,String title) throws IOException
{
ByteArrayInputStream bstream=new ByteArrayInputStream(data);
uploadVideoWithInputStream(userid, developerKey, authtoken, bstream, mimetype, title);
}
public void uploadVideoWithInputStream(String userid,String developerKey,String authtoken,InputStream inputStream,String mimetype,String title) throws IOException
{
try {
String mimeType = mimetype;
String videoTitle = title;
VideoEntry newEntry = new VideoEntry();
YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Tech"));
mg.setTitle(new MediaTitle());
mg.getTitle().setPlainTextContent(videoTitle);
mg.setKeywords(new MediaKeywords());
mg.getKeywords().addKeyword("gdata-test");
mg.setDescription(new MediaDescription());
mg.getDescription().setPlainTextContent(videoTitle);
MediaStreamSource mss=new MediaStreamSource(inputStream, mimeType);
mss.setName(title);
newEntry.setMediaSource(mss);
service.insert(new URL(VIDEO_UPLOAD_FEED), newEntry);
System.out.println("Video uploaded successfully!");
} catch (ServiceException se) {
System.out.println("Sorry, your upload was invalid:");
se.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
uploadVideoWithFileSource(userid,developerid,authtoken,v_path,v_Mimetype,v_Title);
byte[] data=new byte[100];
uploadVideoWithByteData(userid,developerid,authtoken,data,v_Mimetype,v_Title);
}
}
Final Keyword in java
Final
Final is keyword in java
Final With Variable
Final Variable can be declared either block level or instance level
Final Variable can be initialized only once. Re-initialization causes a compile time error
Final Method
Final Methods behaves like constants these methods are not allowed to override in child classes
Final Methods won’t participate with abstract,private Keywords
Final Class
Final Class behaves like constant
Final Class can’t act as a base class in inheritace
Difference between String/String Buffer/String Builder
String
Immutable Object
It contains non-synchronized methods
String Buffer
Mutable Object
It contains synchronized methods
String Builder(from 1.5)
Mutable Object
It contains non-synchronized methods
Yep.... I couldn't have said it better myself......
Yep.... I couldn't have said it better myself......
Yahoo results... While searching Yahoo I found this page in the results and I didn't think
Nice Focus.... I really like the direction you've decided to take this blog....