Archive for March, 2009

Thread Status

threads status

threads status

Some working experience

Whenever you get a new task, no matter how big or difficult it is, do it with all the best you can do. Avoid to use other’s libraries, because they are not stable, and you can not learn the essential programming skills.

Learn how to listen other people’s voice. Learn how to think for other people.

Whenever you get a task, do it good, therefore, the mean people can not pick your fault, and can not push you.

Never say a lot, listen, listen, listen…..

Don’t complain anything, the environment is fair for everybody. You need to learn how to fit, before you change it.

Servlet redirect to a file download with name changed

In this Servlet Class, I put a function named getContentType to get the file type’s name. It is required that the mimeType is set when redirect to a file download.

public class FileServlet extends HttpServlet {
    String getContentType(String fileName) {
        String extension[] = { // File Extensions
            "txt", //0 - plain text
            "htm", //1 - hypertext
            "jpg", //2 - JPEG image
            "png", //2 - JPEG image
            "gif", //3 - gif image
            "pdf", //4 - adobe pdf
            "doc", //5 - Microsoft Word
            "docx",
        }; // you can add more
        String mimeType[] = { // mime types
            "text/plain", //0 - plain text
            "text/html", //1 - hypertext
            "image/jpg", //2 - image
            "image/jpg", //2 - image
            "image/gif", //3 - image
            "application/pdf", //4 - Adobe pdf
            "application/msword", //5 - Microsoft Word
            "application/msword", //5 - Microsoft Word
        }, // you can add more
                contentType = "text/html";    // default type
        // dot + file extension
        int dotPosition = fileName.lastIndexOf('.');
        // get file extension
        String fileExtension =
                fileName.substring(dotPosition + 1);
        // match mime type to extension
        for (int index = 0; index < mimeType.length; index++) {
            if (fileExtension.equalsIgnoreCase( extension[index])) {
                contentType = mimeType[index];
                break;
            }
        }
        return contentType;
    }
 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String filename = request.getParameter("filename");
        String realname = request.getParameter("realname");
        String path = "C:\\web\\ftp_file\\";
        String fullPath = path + filename;
        File file = new File(fullPath);
        String contentType = getContentType(filename);
        System.out.println(contentType);
        response.setContentType(contentType);
        response.setHeader("Content-Disposition", "attachment; filename=" + realname);
        int length = (int) file.length();
 
        if (length > Integer.MAX_VALUE) {
        }
 
        byte[] bytes = new byte[length];
 
        FileInputStream fin = new FileInputStream(file);
 
        fin.read(bytes);
 
        ServletOutputStream os = response.getOutputStream();
        os.write(bytes);
        os.flush();
    }
 
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
 
    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
 
    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

Once it is implemented, you can use the following link to redirect the address to a file download:

http://localhost:8080/FileManagement/FileServlet?filename=somefilename&realname=realfilename

Fav5 of this weekend

1. Retro and vintage web designs have become increasingly popular. Ironically, the more and more the web progresses, the more designers start to revert back to old retro styles. For your design inspiration, here are 40 beautifully-designed websites that revert back in time to retro and vintage styles.

retro and vintage example

retro and vintage example

2. In this article, they present 100 free high-quality WordPress themes.

3. Just found this nice tool: Feedjit. You can see who is on your site at the current moment, see the bottom of this page’s right sidebar, yes, it’s you.

4. Read some posts of this blog. The blog owner is a writer, so maybe can learn something about professional writing.

5. U2.

Some fun sites

The following links is from Matt Cutts’s blog, actually I planed to add some thing and change a lot words, but now today is ending, I only have some time to add some other fancy American sites, and just copy some part form Matt’s site.

The book Dreaming in Code‘s author, Scot Rosenberg is a cofounder of the web salon.com, so I checked the site. It’s pretty good, so I got one more place to go when I have some time to enjoy news.

In case you missed it, Google’s webmaster console launched a new Webmaster Tools API a few weeks ago. A few days after that, they announced that Google’s Webmaster Tools are now available in 40 languages. I think every webmaster should be using this free service at this point.

Learn how to make home-made Girl Scout cookies.

Seth Godin hits marketers over the head with his obvious stick in an excellent post:

If your ads work, if you can measure them and they return more profit than they cost, why not keep buying them until they stop working?

And if they don’t work, why are you running them?

The time-tested response is that you’re not sure, that ads are risky, that you can’t tell. …

Digital ads are different (or they should be). You should know cost per click and revenue per click and be able to make a smart guess about lifetime value of a click. And if that’s positive, buy, buy, buy.

And if you don’t know those things, why are you buying digital ads?

Jacob Gube did a great post about 10 ways to improve your website’s performance. Pretty good.

I don’t like this kind of stuff, but it seems that many people are interested in that. Here is the link for objects falling down a whiteboard and reacting to things on the whiteboard.

Michael Zalewski on Google’s security team had a blog post about Google’s new Browser Security Handbook. The stuff about browser security mechanisms is pretty interesting. It’s good to try chrome, but it’s also good that it’s open source, if you are crazy, you probably will check out the source code.

Darren Rowse discusses the 21 things all new camera owners should know. Since I’m gonna get a new camera, good for me to keep this link.

Reto Meier had a good post about programming for Android. It made me hit Amazon to order his Android programming book. While I was there, I picked up Erica Sadun’s book about programming the iPhone. I kind of want to make a movie where the two books fight with each other.

If you want to convert your blog to a different format, the Google Blog Converters project can help. It looks like it can convert back and forth between Blogger, WordPress, MovableType, and LiveJournal. I like that you can use this code to move your data out of (or into!) Blogger, which holds with the “not trapping users’ data” philosophy that Google has.

Exception handling

Here is a little program about handling exceptions. Just test that, if a exception is thrown in one method, not only that method, but also all methods whichs calls the method,  have to declare or throws exceptions.

public class exceptionTest {
 
    private static Exception exception;
 
    public static void main(String[] args) throws Exception {
            callDoOne(); 
    }
 
    public static void doOne() throws Exception {
        throw exception;
    }
 
    public static void callDoOne() throws Exception {
        doOne();
    }
}

The following is also OK, because the super class can be used to catch or handle subclass exceptions:

class myException extends Exception{
 
}
 
public class exceptionTest {
 
    private static Exception exception;
    private static myException myexception;
    public static void main(String[] args) throws Exception {
            callDoOne(); 
    }
 
    public static void doOne() throws myException {
        throw myexception;
    }
 
    public static void callDoOne() throws Exception {
        doOne();
        throw exception;
    }
}

Fav3 of this week

1. Google Analytics

google analytics map overlay

google analytics map overlay

This is a fantistic tools for web administrator. It gives the full report of the website in a perfect way. The most fancy feature is that it give is the map in which your visitors locate.  The following is a snapshot of the map overlay. The bounce rate help you to determine the effectiveness or performance of an entry page. The bounce rate let you know if the user come and left immediately, and so to measure your quality of your post. There are also many other convenient features, so check it out.

2. Mint
mint-trends-figure1
Just try this web site, if you need some software to manage your money. It let you input your bank account, credit card of most banks, and then everything is loaded automatically. It gives the trending figure for analysis. The following is a diagram I get.

3. Java or .NET? According to the Tiobe Programming Community Index, Java is still ranked number 1. But in a recent survey by Info-Tech of 1,900 companies, 12% of enterprises focus exclusively on .NET , and 49% center primarily on .NET. This compares to 3% and 20% of companies that are focused exclusively or primarily on Java.

Requirement Analysis for Centralized File Management System

The following is the requirement summary for building a new centralized file management system (FMS):

1. After merging the central FMS, each individual system will use web service to connect to it. And all files and related information will be put in a central place.

2. The central should have the following functions:

a. Authenticate

b. Upload (with all related information, such as file name, version number and category)

c. Update (reversion)

d. ListFile (all versions of a file)

e. Download

f. Delete

g. Rename

h. AccessVerify

3. Let each system change as few as possible, when merging the central FMS.

To issustrate the situation, please read the following diagram.

fms requirement Analysis

fms requirement Analysis