Storing the contents of a URL to a file

This example shows how to store the contents of a URL to a File.

URLFetcher.java:

import java.io.*;
import java.net.URL;

public class URLFetcher {
  private String urlString;
  private String fileName;
  
  public URLFetcher(String urlString, String fileName) {
    this.urlString = urlString;
    this.fileName = fileName;
  }

  public void fetch() throws Exception {
    byte buffer[] = new byte[1024]; // change 1024 to better match your needs
    int numRead = 0;
    URL url = new URL(this.urlString);
    OutputStream os = new FileOutputStream(new File(this.fileName));
    InputStream is = url.openStream();
    while ( (numRead = is.read(buffer,0,1024)) != -1) {
      os.write(buffer, 0, numRead);
    }
    is.close();
    os.close();
  }
}

Using the URLFetcher:

URLFetcher urlFetcher = new URLFetcher("myUrl", "myFileName");
try {
  urlFetcher.fetch();
} catch(Exception e) {
  e.printStackTrace();
}

 

Note: This article originally appeared in my Java blog (javanature.com).  I’m shutting that one down, so I’m moving the good stuff that is left over here!

Strip HTML Tags in Java

I came upon a situation in which I needed to find out if String was "really" empty (not just spaces and junk).  The problem was that the String might have empty HTML tags, like <p></p>.  This handy function removes all the HTML tags, leaving any real content.

function removeHtmlTags(String original) {
    return original.replaceAll("\<.*?>", "");
}

The results?

removeHtmlTags("<p></p>")  returns the empty string, but

removeHtmlTags("<p>foo</p>")  returns "foo"

Note: This article originally appears in my Java blog (javanature.com).  I’m shutting that one down, so I’m moving the good stuff that is left over here!

Using the @PathVariable annotation in Spring MVC 3.0

Let’s say that you are creating an user account-based web application in Spring 3.0.  You would like to create a controller that would allow users to see each other’s profile by using a URL such as http://mysite/profile/username.  For example:

http://mysite/profile/Adam  would show user Adam’s profile.

http://mysite/profile/Baker would show user Baker’s profile.

Spring 3.0’s annotated controllers make this kind of mapping easy with the new @PathVariable mapping.

package com.technologicaloddity.hellowebapp;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ShowProfileController {

    @RequestMapping("/profile/{username}")
    public String showProfile(Model model, @PathVariable("username") String username) {
        model.addAttribute("username", username);
        return "showProfile"; // the view name
    }
    
}

As you can see, the RequestMapping for the showProfile method has something a bit unusual: {username}.  This is a PathVariable, and it means that this method will serve any request of the format "/profile/someUserName".  We capture the actual username in the next line using the @PathVariable annotation, and store it in the String username.  Now we can use it however we want!

Hello World Web App using Spring Annotations Part 6

In part 6, the final part of this series, we’ll create the View to display our message to the end-user.  We’ll also run the whole app under Tomcat and Sysdeo, and even show a bit of Java class hot-swapping.  Lastly, we will create a WAR file from our project.

If you have missed the previous parts of this series, you can find them here: part 1, part 2, part 3, part 4, part 5.  You’ll need to setup your environment from parts 1-4 if you want to follow along with the examples here.

You can download the full source of this project from http://technologicaloddity.com/code/general/HelloWebApp.zip.

Continue reading Hello World Web App using Spring Annotations Part 6

Hello World Web App using Spring Annotations Part 5

In Part 5 of this series, we finally get to write some Java!  We’ll create a simple Spring controller using Annotations, using the web application requirements from part 1.

If you have missed the previous parts of this series, you can find them here: part 1, part 2, part 3, part 4.  You’ll need to setup your environment from parts 1-4 if you want to follow along with the examples here.

Continue reading Hello World Web App using Spring Annotations Part 5