reset password
Author Message
cysun
Posts: 2935
Posted 12:21 Mar 14, 2017 |

In Homework 2 you are asked to create a File class, and there are a couple of things I'd like to clarify.

First, the class has the same name as the java.io.File class. As you probably found out, you cannot import both classes because then the compiler won't know which File class a statement like "File file;" refers to. The solution is to import one of them (usually the one that is used more often in your code), and use the "full name" of the other one. For example, you can import your own File class (e.g. "import cs3220.model.File"), and whenever you need to use the other File class, just say java.io.File, .e.g. "java.io.File diskFile = new java.io.File()". The morale of the story is that you can always use the full name (i.e. package + name) of a class without first importing it.

And of course, another solution would be simply naming your File class MyFile or something.

Second, the type field of the homework File class (not the java.io.File) is intended to hold the "content type" of a file. Content type is something like "text/html" for HTML pages or "img/jpeg" for JPG images - it is not the file suffix. Setting content type for HTTP responses (i.e. response.setContentType()) is useful because it tells a browser how to handle an HTTP response properly, but it's not required as browsers are very good at figuring out content type by themselves.

When you handle an uploaded file, you can get the content type by calling the getContentType() method in the FileItem class. Unfortunately there's not an easy way to get the content type of a file on disk (java.io.File doesn't have a getContentType() method), so if you put some files in /WEB-INF/files for testing purposes, you can leave the type field of those files empty.