JAVA 는 http POST 를 이용 하여 파일 을 업로드 합 니 다.
5284 단어 JAVA;WEB
import java.io.*;
import java.net.*;
public class upload {
private static byte[] readFile(String file)throws Exception
{
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[256];
int l;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((l=fis.read(b)) != -1){
bos.write(b,0,l);
}
fis.close();
return bos.toByteArray();
}
public static void main(String[] args) throws Exception{
URL url = new URL("http://ustor.cn/newupload/");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
//
connection.setRequestMethod("POST");
connection.setDoOutput(true);
// byte[]
//File file=new File("d:\\s.txt");
//FileOutputStream fos=new FileOutputStream(file);
/*
byte buff;
StringBuffer file_buff =new StringBuffer();
while((buff=fis.read())!=-1)
{
file_buff.append(new String(buff));
}
*/
//byte[] f="abcdefghijklmnopqrstuvwxyz".getBytes();
//byte[] f=readFile("d:\\s.txt");
byte[] f=new FileUtil().getFileByte("E:\\My Documents\\Desktop\\ \\ _ .jpg");
System.out.println(" :"+f.length);
/*
for(int i=0;i<f.length;i++)
{
System.out.print(f[i]);
}
*/
//fos.write(f);
//
String BOUNDARY = "---------------------------265001916915724";
StringBuffer sb = new StringBuffer();
// :
sb = sb.append("--");
sb = sb.append(BOUNDARY);
sb = sb.append("\r
");
sb = sb.append("Content-Disposition: form-data; name=\"file\"; filename=\" _ .jpg\"\r
");
sb = sb.append("Content-Type: image/jpeg\r
\r
");
byte[] data = sb.toString().getBytes();
byte[] end_data = ("\r
--" + BOUNDARY + "--\r
").getBytes();
// HTTP :
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ BOUNDARY);
connection.setRequestProperty("Content-Length", String.valueOf(data.length + f.length + end_data.length));
// :
OutputStream out=connection.getOutputStream();
out.write(data);
out.write(f);
out.write(end_data);
out.flush();
// :
BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line=null;
while((line=in.readLine())!=null){
System.out.println(line);
}
in.close();
out.close();
}
}
class FileUtil {
public byte[] getFileByte(String fileName) throws FileNotFoundException {
FileInputStream fileInputStream = new FileInputStream(fileName);
return getFileByte(fileInputStream);
}
public byte[] getFileByte(URL url) throws IOException {
if (url != null) {
return getFileByte(url.openStream());
} else {
return null;
}
}
public byte[] getFileByte(InputStream in) {
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
try {
copy(in, out);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
private void copy(InputStream in, OutputStream out) throws IOException {
try {
byte[] buffer = new byte[4096];
int nrOfBytes = -1;
while ((nrOfBytes = in.read(buffer)) != -1) {
out.write(buffer, 0, nrOfBytes);
}
out.flush();
} catch (IOException e) {
}finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
}
try {
if (out != null) {
out.close();
}
} catch (IOException ex) {
}
}
}
}
FireFox 플러그 인:HttpFox 와 Live HTTP header 를 결합 하여 요청 한 머리 를 설정 할 수 있 습 니 다.