我想在tomcat5.5中做以下事情
1. upload a excel file
2. process the file based on some crieteria
3. show the result我可以完成从2到3的所有操作,但不能在tomcat5.5中上传文件,也找不到示例。
请帮帮我。
发布于 2012-06-20 19:30:01
也许你可以试试Apache commons fileUpload
您可以获得一个示例here
一个更多的实践,没有太多的概念和澄清的事情可以在here找到。
在您的Servlet上,您只需使用如下内容:
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator iterator = items.iterator();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
String root = getServletContext().getRealPath("/");
File path = new File(root + "/uploads");
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
System.out.println(uploadedFile.getAbsolutePath());
item.write(uploadedFile);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}发布于 2012-06-20 19:32:13
Apache提供了一个上传文件的API。你可以试试这个。
http://commons.apache.org/fileupload/using.html
发布于 2012-06-20 19:32:58
使用Apache的Commons FileUpload和HttpClient。
这里有一些链接可以帮你解决这个问题。
https://stackoverflow.com/questions/11118456
复制相似问题