我是servlet技术的新手。我想将文件从本地文件系统(即客户端计算机)上传到运行在Tomcat上的服务器。有人可以告诉我怎么做。
我使用html输入元素类型file
(<input type="file"...>
)和form action
属性将数据发布到servlet。
请帮助我。
答案 0 :(得分:3)
Apache.org现在似乎遇到了一些问题,所以here's the Google cached page。
答案 1 :(得分:1)
这不包含在servlet api中,但可以通过http://commons.apache.org/fileupload/
获得基本上,这使用servlet API中包含的IO流来处理上传的流数据 - 我认为计划在最新版本的API中包含文件上传功能,但这一直对我有用。
答案 2 :(得分:1)
最好的选择是使用一些可以处理文件上传的外部库。如果您使用的是Spring,请查看此页面:http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch16s08.html。
Spring在内部使用Apache Commons进行文件上传,所以如果你不使用Spring,或者你只想坚持使用Servlet API,我建议也使用Commons:http://commons.apache.org/fileupload/
答案 3 :(得分:1)
使用文件Upload Component从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();
}
}
}
}