java代码
jsp代码
[color=blue]servlet框架
1.enctype="multipart/form-data" 上传文件的封装
2.form表单的传值 method=“post” get会报错
java.lang.NegativeArraySizeException
3.resp.setCharacterEncoding("gb2312");
<%@ page contentType="text/html;charset=gb2312" language="java" %>
两处的编码方式,当出现中文字符出现乱码时会报错Total File Size exceeded (1110).
4.
[/color]
package test;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class WebFileMgr extends HttpServlet{
private static final long serialVersionUID = 1L;
@SuppressWarnings("deprecation")
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setCharacterEncoding("gb2312");
int count = 0;
//声明一个上传组价对象
SmartUpload su = new SmartUpload();
//初始化
su.initialize(getServletConfig(),req,resp);
//设置允许的上传文件格式
su.setAllowedFilesList("jpg,JPG,bmp,BMP,gif,GIF,jpeg,JPEG,png,PNG,tif,TIF");
su.setTotalMaxFileSize(10000000);
try {
su.upload();//接受上传的文件数据
String uploadfile = req.getRealPath("upload");
File upfile= new File(uploadfile);
if(!upfile.exists()){
upfile.mkdir();
System.out.println("目录创建成功");
}
Files files = su.getFiles();
for(int i=0;i<files.getCount();i++){
com.jspsmart.upload.File file= files.getFile(i);
if(!file.isMissing()){
count = su.save("upload");
}
}
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(count+"file(s) uploaded");
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}
}
jsp代码
<% String path=request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="com.jspsmart.upload.SmartUpload" %>
<%@ page import="test.WebFileMgr" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=gb2312" language="java" %>
<html>
<head>
<base href="<%=basePath%>">
<title>test</title>
<script type="text/javascript" src="<%=path %>/js/jquery-1.4.3.js"></script>
<META content="MSHTML 6.00.2900.2963" name=GENERATOR>
</head>
<body>
path:<%=path %><br>
basePath:<%=basePath %>
<form action="/test/upload" enctype="multipart/form-data" id="form1" name="form1" method="post">
<input type="file" name="file" size="60" />
<input type="button" value="确认" onclick="upload()"/>
</form>
</body>
</html>
[color=blue]servlet框架
1.enctype="multipart/form-data" 上传文件的封装
2.form表单的传值 method=“post” get会报错
java.lang.NegativeArraySizeException
3.resp.setCharacterEncoding("gb2312");
<%@ page contentType="text/html;charset=gb2312" language="java" %>
两处的编码方式,当出现中文字符出现乱码时会报错Total File Size exceeded (1110).
4.
[/color]