import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import java.io.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.FileItem;
import java.util.List;
import java.util.Iterator;
import org.apache.commons.fileupload.*;
import java.util.ResourceBundle;
public class uploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
System.out.println(request.getParameter("test" ));
doProcess(request, response);
return null;
}
protected void doProcess(HttpServletRequest req, HttpServletResponse resp) {
String dirImg = "chemin_sur_le_serveur" ;
HttpServletRequest request = req;
String fileName = "";
InputStream is = null;
DiskFileItemFactory factory = new DiskFileItemFactory(4096000,
new File(System.getProperty("java.io.tmpdir" )));
ServletFileUpload sfu = new ServletFileUpload(factory);
List<FileItem> fileItems = null;
try {
fileItems = sfu.parseRequest(request);
} catch (FileUploadException ex) {
}
Iterator<FileItem> it = fileItems.iterator();
while (it.hasNext()) {
FileItem fileItem = it.next();
if (!fileItem.isFormField()) {
fileName = fileItem.getName();
System.out.println(fileName);
try {
is = fileItem.getInputStream();
OutputStream os = new FileOutputStream(
dirImg + fileName);
int bytesRead = 0;
byte[] buffer = new byte[512 * 1024];
while ((bytesRead = is.read(buffer, 0, 512 * 1024)) != -1) {
os.write(buffer, 0, bytesRead);
} //fin while
os.close();
is.close();
} catch (IOException ex1) {
}
}
} |