[quote=s]Consegui enviar a imagem para uma pasta no meu pc .
só q nao estou conseguindo enviar o diretorio para o banco de dados (mysql)
Gostaria de modificar esse codigo
Alguem por favor me da uma luz ?
Minha index:
[code]<%@ page language=“java” contentType=“text/html; charset=utf-8”
pageEncoding=“utf-8”%>
[/code]
Controller:
[code]package control;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
public class Upload {
public boolean anexos(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (ServletFileUpload.isMultipartContent(request)) {
int cont = 0;
ServletFileUpload servletFileUpload = new ServletFileUpload(
new DiskFileItemFactory());
List fileItemsList = null;
try {
fileItemsList = servletFileUpload.parseRequest(request);
} catch (FileUploadException e1) {
e1.printStackTrace();
}
String optionalFileName = "";
FileItem fileItem = null;
Iterator it = fileItemsList.iterator();
do {
cont++;
FileItem fileItemTemp = (FileItem) it.next();
if (fileItemTemp.isFormField()) {
if (fileItemTemp.getFieldName().equals("filename")) {
optionalFileName = fileItemTemp.getString();
}
} else {
fileItem = fileItemTemp;
}
if (cont != (fileItemsList.size())) {
if (fileItem != null) {
String fileName = fileItem.getName();
if (fileItem.getSize() > 0) {
if (optionalFileName.trim().equals("")) {
fileName = FilenameUtils.getName(fileName);
} else {
fileName = optionalFileName;
}
String dirName = "C:/Users/Administrador/Documents/NetBeansProjects/exupload"; //caminho para o projeto
File saveTo = new File(dirName + fileName);
try {
fileItem.write(saveTo);
} catch (Exception e) {
}
}
}
}
} while (it.hasNext());
return true;
} else {
return false;
}
}
}[/code]
Servlet:
[code]/* * To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import control.Upload;
/**
-
Servlet implementation class ServletUpload
*/
@WebServlet("/upload")
public class ServletUpload extends HttpServlet {
private static final long serialVersionUID = 1L;/**
-
@see HttpServlet#HttpServlet()
*/
public ServletUpload() {
super();
}
/**
-
@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
if (new Upload().anexos(request, response)) {
out.print(“Arquivo enviado!”);
} else {
out.print(“Arquivo não enviado!”);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}[/code] [/quote] -
@see HttpServlet#HttpServlet()