⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javatophp.java

📁 Sound recorder application using swing...
💻 JAVA
字号:
/*
	Coded / Copyright 2005 by Bert Szoghy
	webmaster@quadmore.com

	This program is free software; you can redistribute it and/or modify it under the terms
	of the GNU General Public License version 2 as published by the Free Software Foundation;
	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
	the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	See the GNU General Public License for more details. You should have received a copy of
	the GNU General Public License along with this program; if not, write to the
	Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

	This file will upload a file to a PHP script
*/

import java.io.*;
import java.net.*;

public class JavaToPHP
{
	public void start()
	{
		HttpURLConnection conn = null;
		BufferedReader br = null;
		DataOutputStream dos = null;
		DataInputStream inStream = null;
		InputStream is = null;
		OutputStream os = null;
		boolean ret = false;
		String strMessage = "";
		String lineEnd = "\r\n";
		String twoHyphens = "--";
		String boundary =  "*****";

		int bytesRead, bytesAvailable, bufferSize;
		byte[] buffer;
		int maxBufferSize = 1*1024*1024;

		String responseFromServer = "";
		String urlString = GlobalStorage.getURL();

		try
		{
			String strFilename = GlobalStorage.getFilename();
			System.out.println("Beginning uploading: " + strFilename);

			try
			{
				FileInputStream fileInputStream = new FileInputStream( new File(strFilename) );

				URL url = new URL(urlString);
				conn = (HttpURLConnection) url.openConnection();

				// Allow Inputs
				conn.setDoInput(true);

				// Allow Outputs
				conn.setDoOutput(true);

				// Don't use a cached copy.
				conn.setUseCaches(false);

				// Use a post method.
				conn.setRequestMethod("POST");
				conn.setRequestProperty("Connection", "Keep-Alive");

				conn.setRequestProperty("email", GlobalStorage.getEmail());

				conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

				dos = new DataOutputStream( conn.getOutputStream() );

				dos.writeBytes(twoHyphens + boundary + lineEnd);

				dos.writeBytes("Content-Disposition: form-data; name=\"userfile\";"
				+ " filename=\"" + strFilename +"\"" + lineEnd);
				dos.writeBytes(lineEnd);

				// create a buffer of maximum size
				bytesAvailable = fileInputStream.available();
				bufferSize = Math.min(bytesAvailable, maxBufferSize);
				buffer = new byte[bufferSize];

				// read file and write it into form...
				bytesRead = fileInputStream.read(buffer, 0, bufferSize);

				while (bytesRead > 0)
				{
					dos.write(buffer, 0, bufferSize);
					bytesAvailable = fileInputStream.available();
					bufferSize = Math.min(bytesAvailable, maxBufferSize);
					bytesRead = fileInputStream.read(buffer, 0, bufferSize);
				}

				// send multipart form data necesssary after file data...
				dos.writeBytes(lineEnd);
				dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

				// close streams
				fileInputStream.close();
				dos.flush();
				dos.close();
				System.out.println("Finished uploading: " + strFilename);
			}
			catch (MalformedURLException ex)
			{
				System.out.println("Got a malformed URL:" + ex);
			}
			catch (IOException ioe)
			{
				System.out.println("Got an IO issue:" + ioe);
			}
		}
		catch(Exception e)
		{
			System.out.println("Sorry, could not find the file to upload. Skipping...");
		}

		try
		{
			inStream = new DataInputStream ( conn.getInputStream() );
			String str;
			System.out.println("Server response is:\n");
			while (( str = inStream.readLine()) != null)
			{
				System.out.println(str);
			}

			inStream.close();
		}
		catch (IOException ioex)
		{
			System.out.println("Got an IO issue from (ServerResponse): " + ioex);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -