文件上传之SmartUpload

java代码

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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值