private static final String DESTINATION_DIR_PATH ="/files";
private File destinationDir;
public void init(ServletConfig config) throws ServletException {
super.init(config);
String realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH);
destinationDir = new File(realPath);
if(!destinationDir.isDirectory()) {
throw new ServletException(DESTINATION_DIR_PATH+" is not a directory");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println();
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
fileItemFactory.setSizeThreshold(1*1024*1024);
//fileItemFactory.setRepository(tmpDir);
ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
try {
List items = uploadHandler.parseRequest(request);
Iterator itr = items.iterator();
while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if(item.isFormField()) {
out.println("File Name = "+item.getFieldName()+", Value = "+item.getString());
} else {
File file = new File(destinationDir,item.getName());
item.write(file);
String fileToBeRead = "C:/ProgramFiles/Apache/Tomcat/webapps/Readcsv/files/"+item.getName();
try {
BufferedReader br = new BufferedReader(new FileReader(fileToBeRead));..... and the code goes on..
我正在使用上面的代码读取通过JSP表单上传的.csv文件。代码工作得很好。 但我希望代码是通用格式,因为上面的代码只适用于Windows系统而不是UNIX或任何其他操作系统。如何制作适用于所有操作系统的文件路径通用?
String fileToBeRead = "C:/ProgramFiles/Apache/Tomcat/webapps/Readcsv/files/"+item.getName();
为了完成任务,必须更改此特定行。请让我知道可以做什么,以便代码在任何操作系统中都可以正常工作。也请指出上述代码中需要更改的所有区域。
此代码格式非常难读。如何打开IDE的格式化程序? – 2010-11-02 21:29:14
对不起,我没有使用任何IDE。但是要关注的领域是代码的前两行,以及我已经分别提到的位置。 – LGAP 2010-11-02 21:30:53