2011-03-10 8 views
0

클라이언트 측에서 서버로 pdf를 업로드하고 싶습니다. 나는 서버에서 파일을 업로드하는 방법은 무엇입니까?

<%@ page import="java.io.*" %> 
    <% 

String contentType = request.getContentType(); 

if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))  { 
    DataInputStream in = new DataInputStream(request.getInputStream()); 

    int formDataLength = request.getContentLength(); 
    byte dataBytes[] = new byte[formDataLength]; 
    int byteRead = 0; 
    int totalBytesRead = 0; 

    while (totalBytesRead < formDataLength) 
    { 
     byteRead = in.read(dataBytes, totalBytesRead, formDataLength); 
     totalBytesRead += byteRead; 
    } 

    String file = new String(dataBytes); 

    String saveFile = file.substring(file.indexOf("filename=\"") + 10); 
    saveFile = saveFile.substring(0, saveFile.indexOf("\n")); 
    saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+1,saveFile.indexOf("\"")); 
    int lastIndex = contentType.lastIndexOf("="); 
    String boundary = contentType.substring(lastIndex +1,contentType.length()); 
    int pos; 

    pos = file.indexOf("filename=\""); 
    pos = file.indexOf("\n", pos) + 1; 
    pos = file.indexOf("\n", pos) + 1; 
    pos = file.indexOf("\n", pos) + 1; 
    int boundaryLocation = file.indexOf(boundary, pos) - 4; 
    int startPos = ((file.substring(0, pos)).getBytes()).length; 
    int endPos = ((file.substring(0, boundaryLocation)) 
    .getBytes()).length; 
    saveFile="http://hpws1/shared_mxd/pdf/" + saveFile; 

    FileOutputStream fileOut = new FileOutputStream(saveFile); 
    fileOut.write(dataBytes, startPos, (endPos - startPos)); 
    fileOut.flush(); 
    fileOut.close(); 

    %><Br><table border="2"><tr><td><b>You have successfully 

이의 이름으로 파일을 업로드 ... 다음 코드를 사용 근래 : <%의 out.println (SAVEFILE); %> <% } % "> SAVEFILE ="HTTP

: // hpws1/shared_mxd/PDF/"+ SAVEFILE가 「서버의 경로이다. 이 경로는 브라우저에서 인식하지 못하고 "파일 이름, 디렉터리 이름 또는 볼륨 레이블 구문이 잘못되었습니다."라는 오류 메시지가 나타납니다. 어떻게 해결할 수 있습니까 ???

+0

파일을 저장 하시겠습니까? –

답변

0

왜 양식을 사용하여 파일을 보내지 않으십니까? 이와 같이 :

+0

물론 나는 근사한 모습을 보여줍니다. 그리고 그것은 올바르게 작동합니다. 문제는 : 위의 코드는 로컬 시스템 자체에 파일을 저장하면 작동하지만 서버 위치 "http : // hpws1/shared_mxd/pdf /"에 저장하려면이 코드가 작동합니다. .. – tamanna

+0

아무도 내게 문제를 알릴 수 있습니까 ??? – tamanna

+0

고맙습니다.하지만 찾으십시오. .. – tamanna

관련 문제