Über Formular Bild auf FTP uploaden u. Strings in ne DB

Status
Nicht offen für weitere Antworten.
N

NeoTheHacker

Gast
Hallo Leute,

bin nun soweit, dass ich (mehr oder weniger) eine FTP Klasse fertig geschrieben habe, in der Methode gibt man den Bildnamen an und einen String, wenn zB das Bild in diesen String umbenannt werden soll:

Code:
package upload;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author NeoTheHacker
 * @version 1.0
 * ftpupload
 *
 */
public class FTPUpload2
{

	private static String FTP_SERVER = "server-xyz.petrol.com";
	private static String FTP_USER = "petrol";
	private static String FTP_PASSWORD = "gazprom";
	public static String FTP_DIRECTORY = "/mediadatabase/img_upload/";
	
	String newName = new String();
	String upl_file = new String();
	File objToFtpPut = new File(upl_file);
	
	public void renameAndMoveToFTPServer(String newName, File objToFtpPut)throws IOException
	{
		this.newName = newName;
		this.objToFtpPut = objToFtpPut;
		
		com.oroinc.net.ftp.FTPClient ftp = null;
		FileInputStream inputLocal = null;
		BufferedInputStream inputLocalBuf = null;
		
		float filesize = (objToFtpPut.length()/1024);
		
		try
		{
			ftp = new com.oroinc.net.ftp.FTPClient();
			ftp.connect(FTP_SERVER);
			int reply = ftp.getReplyCode();
			if(!com.oroinc.net.ftp.FTPReply.isPositiveCompletion(reply))
				throw new java.io.IOException("Could not connect to FTP-SERVER "+FTP_SERVER);
			ftp.login(FTP_USER,FTP_PASSWORD);
			ftp.setFileType(com.oroinc.net.ftp.FTP.BINARY_FILE_TYPE);    
			ftp.changeWorkingDirectory(FTP_DIRECTORY);
			try
			{
				inputLocal = new FileInputStream(objToFtpPut);
				inputLocalBuf = new BufferedInputStream(inputLocal);
				if(!ftp.storeFile(newName,inputLocalBuf))
					throw new java.io.IOException("Copy file to remote FTP-SERVER failed!");
			}
			finally
			{
				if(inputLocalBuf!=null) inputLocalBuf.close();
				if(inputLocal!=null) inputLocal.close();
				
				//Successful state
				System.out.println("Your UPLOAD - FILE has a size of "+filesize + " KBytes.");				
				System.out.println("---> SUCCESS - "+objToFtpPut.toString() + " has been uploaded!!!");
				System.out.println("---> Upload Directory is currently " + FTP_DIRECTORY);
			}
			ftp.logout();
		}
		finally
		{
			if(ftp != null)
			{
				try
				{
					ftp.disconnect();
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}
		}
	}

	public static String getFTP_DIRECTORY() {
		return FTP_DIRECTORY;
	}

	public static void setFTP_DIRECTORY(String ftp_directory) {
		FTP_DIRECTORY = ftp_directory;
	}

	public String getNewName() {
		return newName;
	}

	public void setNewName(String newName) {
		this.newName = newName;
	}

	public String getUpl_file() {
		return upl_file;
	}

	public void setUpl_file(String upl_file) {
		this.upl_file = upl_file;
	}

	
}

Mein Formular in der Upload.jsp sieht ca. so aus: Momentan müsste mal ganz einfach der Bilderupload über dieses Formular auf den FTP Server funktionieren:


Code:
...
<form enctype="multipart/form-data" method="post" action="<%=response.encodeURL(request.getContextPath()+"/WebController?page=fileuploadaction")%>">
<input type="file" name="file" id="file" />
<input type="submit" value="Upload" />	   				 	     		
</form>
...

Habe mittels dieser Klasse schon mal probiert auf das File zuzugreifen bzw. auf das Multipart-Encoded od. Textvalues...

Weiß wer vielleicht, wie ich das am geschicktesten anstelle???

Code:
package upload;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * @author NeoTheHacker
 * @version 1.0
 *
 */
public class TextFieldValues
{

	/**
	 * Retrieves the values of the text fields in the form.
	 * It is not possible to get this values by "request.getParameter(...)" in the usual way because
	 * of the enctype in forms which habndle file uploads. Instead of using the normal way,
	 * you have to use this method which uses methods of commons fileupload.
	 * @param request
	 */
	private void getTextFieldValues(HttpServletRequest request)
	{
		FileItemFactory fileItemFactory = new DiskFileItemFactory();
		FileUploadBase fileUploadBase = new ServletFileUpload(fileItemFactory);
		List formItems = null;

		try
		{
			fileItemFactory = fileUploadBase.getFileItemFactory();
			formItems = fileUploadBase.parseRequest(request);
		} catch (FileUploadException e)
		{
//			TODO Auto-generated catch block
			e.printStackTrace();
		}
		Iterator iter = formItems.iterator();

		while (iter.hasNext())
		{
			FileItem fileItem = (FileItem) iter.next();
			if (fileItem.isFormField())
			{
				if (fileItem.getFieldName().equalsIgnoreCase("file"))
				{
					System.out.println(fileItem.getString());
				}
			}
		}
	}  
}

Also, wie kann ich mittels dieser ganzen Infos ein File uploaden, könnt ihr mir noch helfen, viell. mach ich was falsch oder übersehe etwas. Bin auch schon total müde, danke aber mal fürs durchlesen und bis dann, hoffe ich konnte auch Leuten helfen, die genausowelche Klassen suchen ;) da bin ich mir auch sicher ;)

Aber bitte dann auch kurz erklären für die anderen und für mich obs auch funktioniert hat!

thx, mfg NeoTheHacker
 
N

NeoTheHacker

Gast
Danke für die rege Anteilnahme und Hilfe - bin schon selbst draufgekommen... 8)
 
G

Guest

Gast
Hi!

Sitze gerade an einem ähnlichen Problem! wenn ich was weiß melde ich mich!

Gruß
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen

Ähnliche Java Themen

Neue Themen


Oben