client source
private String newName = "1.jpg";
private String uploadFile = "/sdcard/forlder/1.jpg";
private String actionUrl = "http://ipadress:8080/TServer/Test";
private void uploadFile() {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
URL url = new URL(actionUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; "
+ "name=\"file1\";filename=\"" + newName + "\"" + end);
ds.writeBytes(end);
FileInputStream fStream = new FileInputStream(uploadFile);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
while ((length = fStream.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
fStream.close();
ds.flush();
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
Log.e("upload success", b.toString().trim());
ds.close();
} catch (Exception e) {
Log.e("upload failed", e.toString().trim());
}
servlet source
String temp = request.getSession().getServletContext().getRealPath("/")
+ "temp"; //
System.out.println("temp=" + temp);
String loadpath = request.getSession().getServletContext()
.getRealPath("/")
+ "Image"; //
System.out.println("loadpath=" + loadpath);
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(1 * 1024 * 1024); //
fu.setSizeThreshold(4096); //
fu.setRepositoryPath(temp); //
int index = 0;
List fileItems = null;
try {
fileItems = fu.parseRequest(request);
System.out.println("fileItems=" + fileItems);
} catch (Exception e) {
e.printStackTrace();
}
Iterator iter = fileItems.iterator(); //
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();//
if (!item.isFormField()) {
String name = item.getName();//
name = name.substring(name.lastIndexOf("\\") + 1);//
long size = item.getSize();
if ((name == null || name.equals("")) && size == 0)
continue;
int point = name.indexOf(".");
name = (new Date()).getTime()
+ name.substring(point, name.length());//+ name.substring(point, name.length())+index
index++;
File fNew = new File(loadpath, name);
try {
item.write(fNew);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else//
{
String fieldvalue = item.getString();
//
// String fieldvalue = new
// String(item.getString().getBytes(),"UTF-8");
}
}
String text1 = "11";
//response.sendRedirect("result.jsp?text1=" + text1);
}
주의할점 :
add files in was and web project
commons-fileupload-1.3.jar
commons-io-2.4.jar
--------------------------------------------------------------------------------------------------------------------------
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
public class HttpFileUpTool {
public static void post(String actionUrl, Map<String, String> params,
Map<String, File> files) throws IOException {
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "\r\n";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8";
URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(5 * 1000); //
conn.setDoInput(true);//
conn.setDoOutput(true);//
conn.setUseCaches(false); //
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA
+ ";boundary=" + BOUNDARY);
//
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINEND);
sb.append("Content-Disposition: form-data; name=\""
+ entry.getKey() + "\"" + LINEND);
sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
sb.append(LINEND);
sb.append(entry.getValue());
sb.append(LINEND);
}
DataOutputStream outStream = new DataOutputStream(conn
.getOutputStream());
outStream.write(sb.toString().getBytes());
InputStream in = null;
//
if (files != null){
for (Map.Entry<String, File> file : files.entrySet()) {
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
sb1
.append("Content-Disposition: form-data; name=\"file\"; filename=\""
+ file.getKey() + "\"" + LINEND);
sb1.append("Content-Type: application/octet-stream; charset="
+ CHARSET + LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes());
InputStream is = new FileInputStream(file.getValue());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
outStream.write(LINEND.getBytes());
}
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
outStream.write(end_data);
outStream.flush();
int res = conn.getResponseCode();
if (res == 200) {
in = conn.getInputStream();
int ch;
StringBuilder sb2 = new StringBuilder();
while ((ch = in.read()) != -1) {
sb2.append((char) ch);
}
}
outStream.close();
conn.disconnect();
}
// return in.toString();
}
}
String actionUrl = "";
Map<String, String> params = new HashMap<String, String>();
params.put("title", "strParamValue");
params.put("remark", "strParamValue");
params.put("usemame", "strParamValue");
params.put("usertel", "strParamValue");
params.put("eventime", "strParamValue");
params.put("evenaddress", "strParamValue");
params.put("addtime", "strParamValue");
params.put("edittime", "strParamValue");
params.put("muploadid", "strParamValue");
Map<String, File> files = new HashMap<String, File>();
files.put("1.jpg", new File("/sdcard/1.jpg"));
files.put("3.jpg", new File("/sdcard/3.jpg"));
files.put("6.jpg", new File("/sdcard/6.jpg"));
try {
HttpFileUpTool.post(actionUrl, params, files);
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, e.toString());
}
제목 | 글쓴이 | 날짜 | 조회 |
---|---|---|---|
2003-09-20 |
11777 |
||
2003-09-20 |
11318 |
||
2003-09-20 |
20393 |
||
지구인 |
2010-08-27 |
19256 |
|
지구인 |
2009-09-07 |
13554 |
|
2010-01-29 |
15529 |
||
2004-10-07 |
16634 |
||
2014-01-11 |
2447 |
||
2014-01-07 |
2602 |
||
2014-01-07 |
2389 |
||
2014-01-07 |
2058 |
||
2960 [공개소스] 그누보드 4.36.27 |
2014-01-07 |
1797 |
|
2959 [공개소스] 그누보드 4.36.26 |
2014-01-07 |
1618 |
|
2958 [공개소스] 그누보드 4.36.25 |
2014-01-07 |
1182 |
|
즐거운개굴 |
2013-09-29 |
3009 |
|
2013-09-03 |
4914 |
||
2955 [웹 프로그래밍] ajax 특수문자 |
2013-08-29 |
5373 |
|
2013-08-26 |
4799 |
||
2013-08-25 |
3640 |
||
2013-08-24 |
4170 |
||
2013-08-23 |
4328 |
||
2013-08-23 |
3603 |
||
2013-08-22 |
1894 |
||
2948 [웹 프로그래밍] log4j |
2013-08-10 |
1630 |
|
2013-08-05 |
2156 |
||
2946 [웹 프로그래밍] Spring 기초 |
2013-08-05 |
2097 |
|
2013-07-30 |
1477 |
||
2013-07-23 |
1948 |