java 获取上传文件文件名称_JAVA plupload后台如何获取文件名、实例

该博客详细介绍了如何在Java中使用Servlet处理plupload上传的文件,并演示了如何在后台获取上传文件的文件名。通过ServletFileUpload解析请求,遍历文件项,检查文本域字段以获取文件名,并将分块文件合并成完整文件。
摘要由CSDN通过智能技术生成

SERLERT 后台代码:获取文件名

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.ResourceBundle;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.FileUploadException;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.apache.commons.io.FileUtils;

public class UploadServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

// String repositoryPath;

String uploadPath="";

private static final ResourceBundle bundle = ResourceBundle

.getBundle("config");

public UploadServlet() {

super();

}

protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

doPost(request, response);

}

protected void doPost(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

response.setCharacterEncoding("UTF-8");

Integer chunk = null;// 分割块数

Integer chunks = null;// 总分割数

String tempFileName = null;// 临时文件吿

String fileName = "";

BufferedOutputStream outputStream = null;

if (ServletFileUpload.isMultipartContent(request)) {

try {

DiskFileItemFactory factory = new DiskFileItemFactory();

factory.setSizeThreshold(1024);

// factory.setRepository(new File(repositoryPath));// 设置临时目录

ServletFileUpload upload = new ServletFileUpload(factory);

upload.setHeaderEncoding("UTF-8");

// upload.setSizeMax(5 * 1024 * 1024);// 设置附件朿ħ大小,超过这个大小上传会不成势

List items = upload.parseRequest(request);

for (FileItem item : items) {

if (item.isFormField()) {// 是文本域

if (item.getFieldName().equals("name")) {

tempFileName = item.getString();

} else if (item.getFieldName().equals("chunk")) {

chunk = Integer.parseInt(item.getString());

// System.out.println("当前文件块:" + (chunk + 1));

} else if (item.getFieldName().equals("chunks")) {

chunks = Integer.parseInt(item.getString());

// System.out.println("文件总分块:" + chunks);

}

} else {// 如果是文件类垿

if (tempFileName != null) {

String chunkName = item.getName();

fileName = item.getName();//真实文件名

if (chunk != null) {

chunkName = chunk + "_" + tempFileName;

}

File savedFile = new File(uploadPath, chunkName);

item.write(savedFile);

}

}

}

if (chunk != null && chunk + 1 == chunks) {

outputStream = new BufferedOutputStream(

new FileOutputStream(new File(uploadPath,

fileName)));

// 遍历文件合并

for (int i = 0; i < chunks; i++) {

File tempFile = new File(uploadPath, i + "_"

+ tempFileName);

System.out.println("tempFileName:"+tempFileName);

byte[] bytes = FileUtils.readFileToByteArray(tempFile);

outputStream.write(bytes);

outputStream.flush();

tempFile.delete();

}

outputStream.flush();

}

//System.out.println("newFileName:"+newFileName);

Map m = new HashMap();

System.out.println("newFileName:"+fileName);

m.put("status", true);

m.put("fileUrl", uploadPath+ "/"

);

response.getWriter().write(JSONObject.fromObject(m).toString());

} catch (FileUploadException e) {

e.printStackTrace();

Map m = new HashMap();

m.put("status", false);

response.getWriter().write(JSONObject.fromObject(m).toString());

} catch (Exception e) {

e.printStackTrace();

Map m = new HashMap();

m.put("status", false);

response.getWriter().write(JSONObject.fromObject(m).toString());

} finally {

try {

if (outputStream != null)

outputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

@Override

public void init(ServletConfig config) throws ServletException {

GetPropertiesPam gp = new GetPropertiesPam();

uploadPath =gp.getGJPath();

File up = new File(uploadPath);

if (!up.exists()) {

up.mkdir();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值