表单的ENCTYPE属性值为multipart/form-data,它告诉我们传输的数据要用到多媒体传输协议,由于多媒体传输的都是大量的数据,所以规定上传文件必须是post方法,的type属性必须是file。
实现过程:
package cn.csdn.web.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import cn.csdn.web.c3p0.DBManager_c3p0;
public class Upload2Servlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
try {
//实例化一个文件工厂
DiskFileItemFactory factory=new DiskFileItemFactory();
factory.setRepository(new File("C:\\osp"));
String paramName=null;
String paramValue=null;
//配置上传组件ServletFileUpload
ServletFileUpload upload=new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
upload.setFileSizeMax(1024*1024);
//从request得到所有上传域的列表
Listlist=upload.parseRequest(request);
for(FileItem item:list){
//如果是上传域的文件域
if(item.isFormField()){
//表单普通输入项
paramName = item.getFieldName(); //上传于的Name
// String paramValue=item.getString();
// paramValue=new String(paramValue.getBytes("iso8859-1"),"UTF-8");
paramValue=item.getString("UTF-8");
System.out.println(paramName+"="+paramValue);
}else{
//上传文件处理
String fileName = item.getName();
fileName=fileName.substring(fileName.lastIndexOf("\\")+1); //截取扩展名
System.out.println("name="+fileName);
if(!fileName.equals("")){
// fileName=refactorFileName(fileName);
InputStream in=item.getInputStream();
File file = new File("c:\\"+fileName);
FileOutputStream os=new FileOutputStream(file);
byte[] buf = new byte[1024];
int len=0;
while((len=in.read(buf))>0){
os.write(buf,0,len);
}
os.flush();
os.close();
in.close();
item.delete();
request.setAttribute("message", "文件上传成功");
try {
DataSource ds = DBManager_c3p0.getDataSource();
QueryRunner runner = new QueryRunner(ds);
String sql = "insert into user(name,file) values(?,?)";
Object[] params = {paramValue,fileName};
runner.update(sql, params);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
} catch(FileSizeLimitExceededException e1){
e1.printStackTrace();
request.setAttribute("message", "文件尺寸太大");
}catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("message", "文件上传失败");
}
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
// public String refactorFileName(String fileName){
// return UUID.randomUUID().toString()+"_"+fileName;
// }
}
二、上传文件要注意的有:
注意编码问题 防止出现中文乱码 上边列举出一种
其他的还有临时文件解决问题 临时文件删除问题
解决没有指定文件名的问题
判断获取的文件名是否为空
保存路径问题
如表示url资源时应该用斜杠 “/”
如表示硬盘路径时用斜杠“\\”
为保证服务器安全,上传的文件应禁止用户直接访问,通常保存在应用程序的WEB-INF目录下,或者不受WEB服务器管理的目录
本文主要介绍的就是小编教你servlet+JSP+mysql文件上传的实现方法,如您认为此条经验对你有所帮助,别忘了最后支持一下我,点击“收藏”,“成功”。