import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
@SuppressWarnings("deprecation")
public class UploadServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public UploadServlet() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.getWriter().println("請使用post方法傳送文件");
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
@SuppressWarnings({ "deprecation", "unchecked" })
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
//此處必須設置編碼方式爲GBK,否則會出現文件名、目錄名或是卷標語法不正確的錯誤,使用utf-8也不行,其他編碼方式未嘗試過
response.setCharacterEncoding("GBK");
PrintWriter out=response.getWriter();
File file=null;
DiskFileUpload diskFileUpload=new DiskFileUpload();
try {
List fileItems=diskFileUpload.parseRequest(request);
for(FileItem item:fileItems)
{
if(item.isFormField())
{
//文本域,此處不介意說明
}
//文件域
else {
//獲取原始文件
file=new File(new String(item.getName().getBytes(),"GBK"));
//輸出原始文件路徑,親測file.getAbsolutePath()不能正確輸出全路徑
out.println("文件客戶端位置:"+file.getCanonicalPath()+"
");
//輸出文件路徑,此處設置爲D盤下,可隨意更改
File file1=new File("D:\\",file.getName());
//創建文件夾
file1.getParentFile().mkdir();
//創建文件
file1.createNewFile();
InputStream inputStream=item.getInputStream();
OutputStream outputStream=new FileOutputStream(file1);
try{
byte [] b=new byte[1024];
int len=0;
while((len=inputStream.read(b))>-1)
{
outputStream.write(b,0,len);
}
out.println("已保存文件在"+file1.getAbsolutePath()+"
");
}finally{
inputStream.close();
outputStream.close();
}
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.println("wrong");
}
out.println("解析完畢");
out.flush();
out.close();
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
其中,此處的DiskFileUpload和FileItem屬於commons-fileupload-1.2.1.jar外部包
另外還需下載以下三個包(缺少似乎不可行)
commons-fileupload-1.2.1-javadoc.jar
commons-fileupload-1.2.1-sources.jar
commons-io-1.4.jar
下載地址
將這些包加入到工程路徑\WebRoot\WEB-INF\lib下面即可