Java.Web - JSP文件上传,Servlet接收文件

期末项目 网上书店 需要用到 图片上传功能,研究了下,发现很多知识点和细节,下面一一列举

Jsp 代码:

<form action="<%=basePath%>admin/book/add" method="post" ENCTYPE="multipart/form-data">
    <input type="text" name="book_auth" placeholder="作者"> <br />
    <input type="text" name="book_publish" placeholder="出版社"> <br />
    <input type="text" name="book_ISBN" placeholder="ISBN"> <br />
    <input type="text" name="book_price" placeholder="价格"><br />
    <input type="file" name="uploadify" /> <br /> 
    <input type="submit" value="添加书籍">
</form>

注:这里 ENCTYPE="multipart/form-data" 是设置数据以2进制的编码传输,这样任何文件都可以传了,不过这样存在个问题。(看后文)

Serlet 核心代码:

String book_ISBN = "";
String book_name = "";
String book_auth = "";
String book_publish = "";
String book_price = "";
String booktype_id = "";
String savePath = this.getServletConfig().getServletContext().getRealPath("/") + "bookface\\"; // 图片保存的目录
String firstFileName = "";
String fileRealPath = "";// 文件存放真实地址
File file = new File(savePath);
if (!file.isDirectory()) {
file.mkdirs();
}
try {
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("UTF-8");
// 获取多个上传文件,也就是多个input的值
List fileList = upload.parseRequest(request);
// 遍历上传文件
Iterator it = fileList.iterator();
while (it.hasNext()) {
Object obit = it.next();
if (obit instanceof DiskFileItem) {
DiskFileItem item = (DiskFileItem) obit;
String temp = item.getFieldName();
if ("book_ISBN".equals(temp)) {
book_ISBN = new String(item.getString("utf-8"));
}
if ("book_name".equals(temp)) {
book_name = new String(item.getString("utf-8"));
}
if ("book_auth".equals(temp)) {
book_auth = new String(item.getString("utf-8"));
}
if ("book_publish".equals(temp)) {
                        book_publish = new String(item.getString("utf-8"));
}
if ("book_price".equals(temp)) {
book_price = new String(item.getString("utf-8"));
}
if ("booktype_id".equals(temp)) {
booktype_id = new String(item.getString("utf-8"));
}
//下面为保存图片的代码
String fileName = item.getName();
if (fileName != null) {
firstFileName = item.getName().substring(item.getName().lastIndexOf("\\") + 1);
String formatName = firstFileName.substring(firstFileName.lastIndexOf("."));// 获取文件后缀名
fileRealPath = savePath + book_ISBN + formatName;// 文件存放真实地址
BufferedInputStream in = new BufferedInputStream(item.getInputStream());// 获得文件输入流
                                // 获得文件输出流
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(fileRealPath)));
Streams.copy(in, outStream, true); // 开始把文件写到你指定的上传文件夹
// 上传成功,则插入数据库
if (new File(fileRealPath).exists()) {
System.out.print("写入数据库");
}
}
}
}
} catch (org.apache.commons.fileupload.FileUploadException ex) {
ex.printStackTrace();
System.out.println("没有上传文件");
return;
}

注:(颜色对应代码)

这段代码就是上面所说的问题,form里面的input的值以2进制的方式传过去,所以request就得不到值了。
request.getParameter("");  得到的值永远是null!就这个问题我调了一上午的时间,诶!

这段代码是假如当前item是文件,则就保存…java 的IO操作没什么说的

代码中用到的 DiskFileItemFactory 和 ServletFileUpload 这两个类是 commons-fileupload 这个包下面的,
这个包还需要 commons-io 这个包的支持 网上很容易找到。

下面爆下我做的东西:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值