salve raga ho un problema che non riesco a risolvere da circa 3gg....vi spiego:
ho una pagina jsp (ut1.jsp) nella quale ho un form con un input texarea e un altro type="file",
vorrei fare l'upload in qst caso di un immagine ma nn va in nessun modo....vi posto il codice SPERO KE QLC POSSA DARMI UNA MANO![]()
UT1.JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="ImmFoto" enctype="multipart/form-data">
<h1 align=center>
BENVENUTO "<%=session.getAttribute("nome")%>" NELLA TUA WEBPAGE
PERSONALE</h1>
<TABLE align=center>
<tr>
<td>AGGIUNGI DESCRIZIONE:</td>
<TD><textarea name="text" cols="36" rows="10"> </textarea></TD>
</tr>
<tr>
<td>AGGIUNGI FOTO1:</td>
<TD><input type="file" name="foto1"></TD>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value=Invia><input
type=reset value=Cancella></td>
</tr>
</TABLE>
</form>
</body>
</html>
QST è LA SERVLET:
package it.ats.portale.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
public class WebPersonal extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// Istanzio le variabili
ServletContext context = getServletContext();
String forw = null;
try {
int maxUploadSize = 50000000;
MultipartRequest multi = new MultipartRequest(request,".",maxUploadSize);
String descrizione = multi.getParameter("text");
File foto1 = multi.getFile("foto1");
String filePath = multi.getOriginalFileName("foto1");
String path = "C:\\ImmFoto\\";
try {
//retrieve the file data
FileInputStream inStream = new FileInputStream(foto1);
FileOutputStream outStream = new FileOutputStream(path+foto1.getName());
//write the file to the file specified
while (inStream.available()>0) {
outStream.write(inStream.read());
}
//close the stream
inStream.close();
outStream.close();
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
forw = "/done.jsp";
Request.setAttribute("contentType",context.getMime Type(path+foto1.getName()));
request.setAttribute("text",descrizione);
request.setAttribute("path",path+foto1.getName());
request.setAttribute("size",Long.toString(foto1.le ngth())+" Bytes");
RequestDispatcher rd =request.getRequestDispatcher(forw);
rd.forward(request,response);
}catch(Exception e) {e.printStackTrace();}
}
}
DONE.JSP
<%
String text = (String)request.getAttribute("text");
String path = (String)request.getAttribute("path");
String contentType = (String)request.getAttribute("contentType");
String size = (String)request.getAttribute("size");
%>
<html>
<body bgcolor="#6A95B2">
<center><font face="Tahoma" size="4">FATTO!!!</font></center>
<table border="1">
<tr>
<td>
<font face="Tahoma" size="3">Descrizione:</font>
</td>
<td>
<font face="Tahoma" size="3"><%=text%></font>
</td>
</tr>
<tr>
<td>
<font face="Tahoma" size="3">Percorso sul server:</font>
</td>
<td>
<font face="Tahoma" size="3"><%=path%></font>
</td>
</tr>
<tr>
<td>
<font face="Tahoma" size="3">MIME type:</font>
</td>
<td>
<font face="Tahoma" size="3"><%=contentType%></font>
</td>
</tr>
<tr>
<td>
<font face="Tahoma" size="3">Dimensioni</font>
</td>
<td>
<font face="Tahoma" size="3"><%=size%></font>
</td>
</tr>
</table>
Torna alla pagina Upload
</body>
</html>