Problema de escrita em disco, uploadfile e tomcat

3 respostas
E
Saudações. Estou tentando fazer um servlet (o prof quer servlet, não jsp) que faça um upload de uma figura, porém está dando este erro de escrita: erro:
H:\tomcat-5.0.3\webapps\ROOT\E:\novas\finepixs51004.gif (A sintaxe do nome do arquivo, pasta ou nome do volume está incorreta)
O código do servlet segue abaixo:
package coreservlets;
import java.io.*;
import java.io.File.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.List;
import java.util.Iterator;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.FileUploadBase.*;
import org.apache.commons.fileupload.DiskFileUpload;

public class UploadFileServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String title = "Upload Files Servlet";
		DiskFileUpload fu = new DiskFileUpload();
		fu.setSizeMax(10000000);
		String cType = "";
		try{
			List fItems = fu.parseRequest(request);
		
			Iterator fItr = fItems.iterator();
			
		
			while(fItr.hasNext()){
				FileItem fi = (FileItem)fItr.next();
				cType = fi.getContentType();
				if(!fi.isFormField()){
					out.println(ServletUtilities.headWithTitle(title) + "<body>\n" +
					"<br>Field ="+fi.getFieldName() + "<br>" +
					"File name: " + fi.getName() + "<br>" +
					"File Size: " + fi.getSize() + "<br>" +
					"ContentType: " + cType + "<br>" +
					
					"File Uploaded\n" +
					"</body></html>");
					String fLocation = "/";
					//File upFile = new File(application.getRealPath("/"), fItem.getName());
					File upFile = new File(getServletContext().getRealPath("/"), fi.getName());
					try {
						out.println("<br>Path:" + upFile.getAbsolutePath() + "\n");
						fi.write(upFile);
					} catch ( Exception e ) {
							out.println(ServletUtilities.headWithTitle(title) + "<body>\n" +
							"<br>Erro de escrita(write): " + e.getMessage());
					}
				} else {
					out.println(ServletUtilities.headWithTitle(title) + "<body>\n" +
					"<br>Field ="+fi.getFieldName());
				}
			}
		}catch(FileUploadException e){
			out.println(ServletUtilities.headWithTitle(title) + "<body>\n" +
			"<center> Exception:" + e.getMessage() +
			"</body></html>");
		}

	}	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
		doGet(request, response);
	}
}

Agradeço

3 Respostas

M

cara, veja bem o erro… cheque as suas Strings q esta usando pra referencias o arquivo…

E

De uma olhada neste bean utilizo-o a algum tempo e nunca tive problemas .

http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html

Blz

E

O que não estou conseguindo é encontrar a api para passar o caminho onde será gravado o arquivo, e tem que ser com servlet, se fosse com beans, eu tenho pronto já…
em beans, foi usado application.getRealPath("/") e funcionou, porém o application não pode ser usado no servlet, só em beans…

Valeu
Eliselbert

Criado 5 de maio de 2005
Ultima resposta 5 de mai. de 2005
Respostas 3
Participantes 3