一、 开发环境
tomcat 6.0 + JDK1.7+ fileupload包 + MyEclipse
二、 开发原理
JSP响应流程图
主要技术:java文件操作
tomcat文件上传
JSP网页编程
JavaBean的调用
三、开发过程中问题和解决方法
1.解决MyEclipse下引入的工程无法进行server deploy
这里提到的无法进行server deploy是指在MyEclipse的Server Deployments界面的工程列表中没有出现新导入的工程。
(1)、先看看该工程根目录下是否有.mymetadata这个文件。出现该问题一般都是工程导入MyEclipse之后,MyEclipse不认该工程为web project,这个时候需要为新导入的项目执行“Add Web Project”的操作,执行的菜单路径“MyEclipse”à “Capabilities” à “Add Web Project Capabilities...”。
如果不能执行该操作,则需要在Add Web Project Capabilities界面中,把两个复选框的勾都去掉(create web.xml以及增加j2EE的lib路径到classpath),然后就可以执行了。
实际上工程导入MyEclipse,一般MyEclipse会认出工程是web工程,从而自动创建.mymetadata的。当然如果无法自动创建,就按上面的办法处理。
(2)、有了.mymetadata文件,在执行部署的时候,仍不能成功。项目终于可以在Tomcat下Deploy了。但是还是有问题,Deploy的项目有点不对劲。这是因为MyEclipse默认的Web-Root路径是WebRoot而在Eclipse下是WebContent的原因。打开.mymetadata的文件,可以看到如下内容:
<?xmlversion="1.0" encoding="UTF-8"?>
<project-module
type="WEB"
name="test"
id="myeclipse.1173497999562"
context-root="/test"
j2ee-spec="1.4"
archive="test.war">
<attributes>
<attribute name="webrootdir"value="web-root" />
</attributes>
</project-module>
把web-root改为工程定义的web root dir名称即可,一般为"webapp"。改完之后如果还不能直接发布,重启下MyEclipse就行了。
(3)、在MyEclipse创建一个临时web Project,把里面的.mymetadata拷给部署有问题的工程用就可以了,当然里面的内容要修改一下。临时的web project记得删除掉。
(4)、如果都不可以最后也是亲测的,可以进入project deploymentà add àtomcat.6.0
2.Content-Disposition的使用和注意事项
Content-disposition是MIME 协议的一个扩展。
Content-disposition可以满足以下需求:
• 希望某类或者某已知MIME 类型的文件(比如:*.gif;*.txt;*.htm)能够在访问时弹出“文件下载”对话框
• 希望以原始文件名(上传时的文件名,例如:山东省政府1024号文件.doc)提供下载,但服务器上保存的地址却是其他文件名(如:12519810948091234_asdf.doc)
• 希望某文件直接在浏览器上显示而不是弹出文件下载对话框
• ……………………
第一个需求的解决办法是:
Response.AddHeader"content-disposition","attachment; filename=fname.ext"
将上述需求进行归我给出如下例子代码:
public static voidToDownload(string serverfilpath,string filename)
{
FileStreamfileStream = new FileStream(serverfilpath, FileMode.Open);
long fileSize =fileStream.Length;
HttpContext.Current.Response.ContentType= "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition","attachment; filename=\"" + UTF_FileName(filename) +"\";");
//attachment --- 作为附件下载
//inline --- 在线打开
HttpContext.Current.Response.AddHeader("Content-Length",fileSize.ToString());
byte[] fileBuffer= new byte[fileSize];
fileStream.Read(fileBuffer,0, (int)fileSize);
HttpContext.Current.Response.BinaryWrite(fileBuffer);
fileStream.Close();
HttpContext.Current.Response.End();
}
public static voidToOpen(string serverfilpath, string filename)
{
FileStreamfileStream = new FileStream(serverfilpath, FileMode.Open);
long fileSize =fileStream.Length;
HttpContext.Current.Response.ContentType= "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition","inline; filename=\"" + UTF_FileName(filename) +"\";");
HttpContext.Current.Response.AddHeader("Content-Length",fileSize.ToString());
byte[] fileBuffer= new byte[fileSize];
fileStream.Read(fileBuffer,0, (int)fileSize);
HttpContext.Current.Response.BinaryWrite(fileBuffer);
fileStream.Close();
HttpContext.Current.Response.End();
}
private staticstring UTF_FileName(string filename)
{