3个回答
展开全部
刚学完多文件上传,希望对你有帮助。
多文件上传,就是把多个文件一起传到指定的位置,他和单文件上传类似,只需要改动几个地方而已即可。首先是上传页面,由于要上传多个文件,所以就必须有多个供用户选择的文件框,然后修改Action,把action中属性的类型修改为list。
action如下:
public class UploadActionMany extends ActionSupport{
private List<File> upload;
private List<String> uploadFileName;
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.print(upload.size());
if(upload!=null){
for (int i = 0; i < upload.size(); i++) {
InputStream is=new FileInputStream(upload.get(i)); OutputStream os=new FileOutputStream("d:\\upload\\"+getUploadFileName().get(i)); byte buffer[]=new byte[1024];
int count=0;
while((count=is.read(buffer))>0){
os.write(buffer,0,count);
}
os.close();
is.close();
}
}
return SUCCESS;
}
省略set和get方法
jsp如下:
<html>
<head>
<title>文件上传</title>
</head>
<body>
<s:form action="upload.action" method="post" enctype="multipart/form-data"> <s:file name="upload" label="上传的文件一"></s:file>
<s:file name="upload" label="上传的文件二"></s:file>
<s:file name="upload" label="上传的文件三"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
</body>
</html>
希望对你有帮助,打代码不容易啊,记得给点分
多文件上传,就是把多个文件一起传到指定的位置,他和单文件上传类似,只需要改动几个地方而已即可。首先是上传页面,由于要上传多个文件,所以就必须有多个供用户选择的文件框,然后修改Action,把action中属性的类型修改为list。
action如下:
public class UploadActionMany extends ActionSupport{
private List<File> upload;
private List<String> uploadFileName;
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.print(upload.size());
if(upload!=null){
for (int i = 0; i < upload.size(); i++) {
InputStream is=new FileInputStream(upload.get(i)); OutputStream os=new FileOutputStream("d:\\upload\\"+getUploadFileName().get(i)); byte buffer[]=new byte[1024];
int count=0;
while((count=is.read(buffer))>0){
os.write(buffer,0,count);
}
os.close();
is.close();
}
}
return SUCCESS;
}
省略set和get方法
jsp如下:
<html>
<head>
<title>文件上传</title>
</head>
<body>
<s:form action="upload.action" method="post" enctype="multipart/form-data"> <s:file name="upload" label="上传的文件一"></s:file>
<s:file name="upload" label="上传的文件二"></s:file>
<s:file name="upload" label="上传的文件三"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
</body>
</html>
希望对你有帮助,打代码不容易啊,记得给点分
展开全部
public class Uploads extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
request.setCharacterEncoding("GBK");
try {
SmartUpload su = new SmartUpload();
su.setMaxFileSize(5 * 1024 * 1024);
su.setAllowedFilesList("jpg,gif,bmp,tif");
// su.setDeniedFilesList("rmvb,mkv,avi");
su.initialize(this.getServletConfig(), request, response);
su.upload();
Request myreq = su.getRequest();
String username = myreq.getParameter("myname");
String password = myreq.getParameter("mypass");
java.io.File myfile = new java.io.File(this.getServletContext()
.getRealPath("/images"));
if (!myfile.exists()) {
myfile.mkdir();
}
// 得到全部的上传文件
Files files = su.getFiles();
// 一共有多少个上传文件
int count = files.getCount();
UploaderDaoIf dao = new UploaderDaoImpl();
// 循环遍历上传文件
for (int i = 0; i < count; i++) {
File file = files.getFile(i);
// 得到上传文件的后缀名
String fileExt = file.getFileExt();
String fileName = new UUIDGenerator().generate() + "."
+ fileExt;
String finalPath = "/" + myfile.getName() + "/" + fileName;
Uploader up = new Uploader();
up.setUsername(username);
up.setPassword(password);
up.setPicPath(finalPath);
dao.addUp(up);
file.saveAs(finalPath);
}
response.sendRedirect("/jspDay3_upload/shows.jsp");
} catch (Exception ex) {
ex.printStackTrace();
}
}
培训时的课件用的SERVLET 挺简单的
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
request.setCharacterEncoding("GBK");
try {
SmartUpload su = new SmartUpload();
su.setMaxFileSize(5 * 1024 * 1024);
su.setAllowedFilesList("jpg,gif,bmp,tif");
// su.setDeniedFilesList("rmvb,mkv,avi");
su.initialize(this.getServletConfig(), request, response);
su.upload();
Request myreq = su.getRequest();
String username = myreq.getParameter("myname");
String password = myreq.getParameter("mypass");
java.io.File myfile = new java.io.File(this.getServletContext()
.getRealPath("/images"));
if (!myfile.exists()) {
myfile.mkdir();
}
// 得到全部的上传文件
Files files = su.getFiles();
// 一共有多少个上传文件
int count = files.getCount();
UploaderDaoIf dao = new UploaderDaoImpl();
// 循环遍历上传文件
for (int i = 0; i < count; i++) {
File file = files.getFile(i);
// 得到上传文件的后缀名
String fileExt = file.getFileExt();
String fileName = new UUIDGenerator().generate() + "."
+ fileExt;
String finalPath = "/" + myfile.getName() + "/" + fileName;
Uploader up = new Uploader();
up.setUsername(username);
up.setPassword(password);
up.setPicPath(finalPath);
dao.addUp(up);
file.saveAs(finalPath);
}
response.sendRedirect("/jspDay3_upload/shows.jsp");
} catch (Exception ex) {
ex.printStackTrace();
}
}
培训时的课件用的SERVLET 挺简单的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接用struts2的就行了,批量也不要想的太复杂,就是后台定义成List<File> uploadFile;页面就是<input type="file" name="uploadFile(这里要和你action方法里定义的那个List<File> uploadFile这个属性名一样)"/>,最后后台action方法 里的话就加一个for循环uploadFile就好了。具体struts2的上传去百度搜了,例子太多,记的加分。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询