자바 업로드
표준 httpServletRequest 가 아 닌 서버 를 만 났 습 니 다.
common-uproad 구성 요 소 를 사용 할 수 없습니다.
슬프다
해석 하 다
request.getInputStream()이 이렇게 하 는 구나.
Map<String, String> formfieldsTable = new HashMap<String, String>();
String fileRealName = ""; //
String length="";//
try {
response.setContentType("text/html;charset=UTF-8");
//PrintWriter out = response.getWriter();
final int NONE = 0; // ,
final int DATAHEADER = 1; //
final int FILEDATA = 2; //
final int FIELDDATA = 3; //
// ( )
int totalbytes = request.getContentLength();
File f; //
//
byte[] dataOrigin = new byte[totalbytes];
// post ,b
byte[] b = new byte[totalbytes];
//
String contentType = request.getContentType();
String fieldname = ""; //
String fieldvalue = ""; //
String fileFormName = ""; //
String boundary = ""; //
String lastboundary = ""; //
int fileSize = 0; //
// /
// /
Map<String, String> filenameTable = new HashMap<String, String>();
//
int pos = contentType.indexOf("boundary=");
int pos2; // position2
if (pos != -1) {
pos += "boundary=".length();
boundary = "--" + contentType.substring(pos); //
lastboundary = boundary + "--"; //
}
int state = NONE; // NONE
//
DataInputStream in = new DataInputStream(request.getInputStream());
in.readFully(dataOrigin); // , dataOrigin
in.close(); //
String reqcontent = new String(dataOrigin); //
//
BufferedReader reqbuf = new BufferedReader(new StringReader(
reqcontent));
//
boolean flag = true;
// int i = 0;
while (flag == true) {
String s = reqbuf.readLine();
if (s == lastboundary || s == null)
break;
switch (state) {
case NONE:
if (s.startsWith(boundary)) {
// ,
state = DATAHEADER;
// i += 1;
}
break;
case DATAHEADER:
pos = s.indexOf("filename=");
// ,
if (pos == -1) {
// ,
pos = s.indexOf("name=");
pos += "name=".length() + 1; // 1 "
s = s.substring(pos);
int l = s.length();
s = s.substring(0, l - 1); // "
fieldname = s; // fieldname
System.out.print("fieldname=" + fieldname);
state = FIELDDATA; // ,
} else {
// , ,
String temp = s;
//
pos = s.indexOf("name=");
pos += "name=".length() + 1; // 1 "
pos2 = s.indexOf("filename=");
String s1 = s.substring(pos, pos2 - 3); // 3 ";
fileFormName = s1;
pos2 += "filename=".length() + 1; // 1 "
s = s.substring(pos2);
int l = s.length();
s = s.substring(0, l - 1);
pos2 = s.lastIndexOf("\\"); // IE
s = s.substring(pos2 + 1);
fileRealName = s;
System.out.print("fileRealName=" + fileRealName + "<br>");
System.out.print("fileRealName.length()="
+ fileRealName.length() + "<br>");
if (fileRealName.length() != 0) { //
//
b = dataOrigin; //
pos = byteIndexOf(b, temp, 0); //
// ,2
b = subBytes(b, pos + temp.getBytes().length + 2,
b.length);
// , Content-type
s = reqbuf.readLine();
// ,
f = new File(
"/usr/local/app/author_portal_front/upload"
+ File.separator + fileRealName);
DataOutputStream fileout = new DataOutputStream(
new FileOutputStream(f));
// ,4 4 , 2 ,Content-type
// , 2
//
b = subBytes(b, s.getBytes().length + 4, b.length);
pos = byteIndexOf(b, boundary, 0); //
b = subBytes(b, 0, pos - 1); //
length=b.length+"";
fileout.write(b, 0, b.length - 1); //
fileout.close();
fileSize = b.length - 1; // fileSize
System.out.print("fileFormName=" + fileFormName
+ " filename=" + fileRealName
+ " fileSize=" + fileSize + "<br>");
filenameTable.put(fileFormName, fileRealName);
state = FILEDATA;
}
}
break;
case FIELDDATA:
//
s = reqbuf.readLine();
fieldvalue = s; // fieldvalue
System.out.print(" fieldvalue=" + fieldvalue + "<br>");
formfieldsTable.put(fieldname, fieldvalue);
state = NONE;
break;
case FILEDATA:
// ,
while ((!s.startsWith(boundary))
&& (!s.startsWith(lastboundary))) {
s = reqbuf.readLine();
if (s.startsWith(boundary)) {
state = DATAHEADER;
} else {
break;
}
}
break;
}
}
} catch (Exception e) {
System.out.println("error upload:" + e);
e.printStackTrace();
}
String abc=GetTest.testGet("/usr/local/app/author_portal_front/upload/"+fileRealName , formfieldsTable.get("myname"),length);
System.out.println("------------->uploadUrl:"+abc);
request.setAttribute("intro", abc);
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.