NutzCN Logo
问答 Nutz的http是否支持文件上传?
发布于 570天前 作者 huc_y 646 次浏览 复制 上一个帖子 下一个帖子
标签: http

通过代码模拟带文件的表单上传

7 回复

类似这样

		Request req = Request.create("http://nutz.cn", METHOD.POST);
		req.getParams().put("xxx", new File("...."));
		req.getParams().put("abc", "zcdsfdf");
		Response resp = new FilePostSender(req).setTimeout(60*1000).send();
		if (!resp.isOK()) {
			// .......
		} else {
			// ....
		}

@wendal 单文件上传是可以的,但是多个文件怎么办?并且参数名是同一个。

代码在此:https://nutz.cn/s/c/2x
我修改了关于文件上传的代码,但是还是只能传一个文件

多个文件,但同一个参数,还不支持,报个issue 吧

同key上传多个文件的代码:

package com.dgj.utils.net;

import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;

import org.nutz.http.HttpException;
import org.nutz.http.Request;
import org.nutz.http.Response;
import org.nutz.http.sender.PostSender;
import org.nutz.lang.ContinueLoop;
import org.nutz.lang.Each;
import org.nutz.lang.ExitLoop;
import org.nutz.lang.Lang;
import org.nutz.lang.LoopException;
import org.nutz.lang.Streams;

/**
 * 请勿修改
 * 
 * @author Ixion
 *
 *         create at 2014年8月27日
 */
public class IxionFileUploadPoster extends PostSender {
	public static final String SEPARATOR = "\r\n";

	public IxionFileUploadPoster(Request request) {
		super(request);
	}

	@Override
	public Response send() throws HttpException {
		try {
			final String boundary = "---------------------------[Nutz]7d91571440efc";
			openConnection();
			setupRequestHeader();
			conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
			setupDoInputOutputFlag();
			Map<String, Object> params = request.getParams();
			if (null != params && params.size() > 0) {
				final DataOutputStream outs = new DataOutputStream(conn.getOutputStream());
				for (Entry<String, ?> entry : params.entrySet()) {
					outs.writeBytes("--" + boundary + SEPARATOR);
					final String key = entry.getKey();
					File[] fs = null;
					if (entry.getValue() instanceof File)
						fs = Lang.array((File) entry.getValue());
					else if (entry.getValue() instanceof File[])
						fs = (File[]) entry.getValue();
					if (fs != null) {
						Lang.each(fs, new Each<File>() {

							@Override
							public void invoke(int index, File f, int length) throws ExitLoop, ContinueLoop, LoopException {
								try {
									if (f != null && f.exists()) {
										outs.writeBytes("Content-Disposition:    form-data;    name=\"" + key + "\";    filename=\"" + f.getName() + "\"\r\n");
										outs.writeBytes("Content-Type:   application/octet-stream\r\n\r\n");
										if (f.length() == 0)
											return;
										InputStream is = Streams.fileIn(f);
										byte[] buffer = new byte[8192];
										while (true) {
											int amountRead = is.read(buffer);
											if (amountRead == -1) {
												break;
											}
											outs.write(buffer, 0, amountRead);
										}
										outs.writeBytes("\r\n");
										Streams.safeClose(is);
									}
									outs.writeBytes("--" + boundary + SEPARATOR);
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						});
					} else {
						outs.writeBytes("Content-Disposition:    form-data;    name=\"" + key + "\"\r\n\r\n");
						outs.write((entry.getValue() + "\r\n").getBytes());
					}
				}
				outs.writeBytes("--" + boundary + "--" + SEPARATOR);
				Streams.safeFlush(outs);
				Streams.safeClose(outs);
			}

			return createResponse(getResponseHeader());

		} catch (IOException e) {
			throw new HttpException(request.getUrl().toString(), e);
		}
	}

}

@kerbores 赞!发个pull req?

来自炫酷的 NutzCN

添加回复
请先登陆
回到顶部