2011-03-21 82 views
0

我對servlet技術很陌生。我想從本地文件系統(即客戶機)上傳文件到Tomcat上運行的服務器。有人可以告訴我如何做到這一點。如何使用servlet將文件上傳到服務器?

我使用html輸入元素類型file<input type="file"...>)和form action屬性將數據發佈到servlet。

plz幫助我。

回答

1

這是不包含在Servlet API的,但可通過http://commons.apache.org/fileupload/

基本上,這使用包括IO流了servlet API在處理上載流數據 - 我認爲有計劃,包括文件上傳功能了在最新版本的API中,該框一直在爲我工作。

1

傳遞從JSP文件或使用文件上傳組件的HTML。並在表單動作中設置servlet.java。

Servlet.java

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.sql.*; 

public class UploadServlet extends HttpServlet{ 
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { 
    response.setContentType("text/html"); 
    PrintWriter out = response.getWriter(); 

    String saveFile=""; 
    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); 
    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; 
    File ff = new File(saveFile); 
    FileOutputStream fileOut = new FileOutputStream(ff); 
    fileOut.write(dataBytes, startPos, (endPos - startPos)); 
    fileOut.flush(); 
    fileOut.close(); 
    out.println("You have successfully upload the file:"+saveFile); 
    Connection connection = null; 
    String connectionURL = "jdbc:mysql://localhost:3306/test"; 
    ResultSet rs = null; 
    PreparedStatement psmnt = null; 
    FileInputStream fis; 
    try{ 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    connection = DriverManager.getConnection(connectionURL, "root", "root"); 
    File f = new File(saveFile); 
    psmnt = connection.prepareStatement("insert into file(file_data) values(?)"); 
    fis = new FileInputStream(f); 
    psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length())); 
    int s = psmnt.executeUpdate(); 
    if(s>0){ 
    System.out.println("Uploaded successfully !"); 
    } 
    else{ 
    System.out.println("Error!"); 
    } 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
     } 
    } 
    } 
} 
相關問題