我對servlet技術很陌生。我想從本地文件系統(即客戶機)上傳文件到Tomcat上運行的服務器。有人可以告訴我如何做到這一點。如何使用servlet將文件上傳到服務器?
我使用html輸入元素類型file
(<input type="file"...>
)和form action
屬性將數據發佈到servlet。
plz幫助我。
我對servlet技術很陌生。我想從本地文件系統(即客戶機)上傳文件到Tomcat上運行的服務器。有人可以告訴我如何做到這一點。如何使用servlet將文件上傳到服務器?
我使用html輸入元素類型file
(<input type="file"...>
)和form action
屬性將數據發佈到servlet。
plz幫助我。
Apache.org似乎現在有一些問題,所以here's the Google cached page。
這是不包含在Servlet API的,但可通過http://commons.apache.org/fileupload/
基本上,這使用包括IO流了servlet API在處理上載流數據 - 我認爲有計劃,包括文件上傳功能了在最新版本的API中,該框一直在爲我工作。
最好的選擇是使用一些可以處理文件上傳的外部庫。如果您使用的是Spring,請查看此頁面:http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch16s08.html。
春天在內部使用Apache共享文件上傳,所以如果你不使用Spring,或者您想只要堅持到Servlet API的,我建議使用共享以及:http://commons.apache.org/fileupload/
傳遞從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();
}
}
}
}