标签:content-disposition filename jmeter jmeter源码 jmeter上传
uploader.onBeforeUploadItem = function (item) { //修改名字 var timeStamp = new Date().getTime(); var fileName = item.file.name; item.file.name = timeStamp + fileName.substr(fileName.lastIndexOf(‘.‘)); var day = $filter(‘date‘)(new Date(), ‘yyyyMMdd‘); item.url = [item.url, "batchImport", item.importType, day, session.userId].join("/"); };
/** * Write the start of a file multipart, up to the point where the * actual file content should be written */ private void writeStartFileMultipart(OutputStream out, String filename, String nameField, String mimetype) throws IOException { write(out, "Content-Disposition: form-data; name=\""); // $NON-NLS-1$ write(out, nameField); write(out, "\"; filename=\"");// $NON-NLS-1$ write(out, new File(filename).getName()); writeln(out, "\""); // $NON-NLS-1$ writeln(out, "Content-Type: " + mimetype); // $NON-NLS-1$ writeln(out, "Content-Transfer-Encoding: binary"); // $NON-NLS-1$ out.write(CRLF); }
try { conn = setupConnection(url, method, res); // Attempt the connection: savedConn = conn; conn.connect(); break; } catch (BindException e) { if (retry >= MAX_CONN_RETRIES) { log.error("Can‘t connect after "+retry+" retries, "+e); throw e; } log.debug("Bind exception, try again"); if (conn!=null) { savedConn = null; // we don‘t want interrupt to try disconnection again conn.disconnect(); } setUseKeepAlive(false); continue; // try again } catch (IOException e) { log.debug("Connection failed, giving up"); throw e; } } if (retry > MAX_CONN_RETRIES) { // This should never happen, but... throw new BindException(); } // Nice, we‘ve got a connection. Finish sending the request: if (method.equals(HTTPConstants.POST)) { String postBody = sendPostData(conn); res.setQueryString(postBody); }
protected HttpURLConnection setupConnection(URL u, String method, HTTPSampleResult res) throws IOException { SSLManager sslmgr = null; if (HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(u.getProtocol())) { try { sslmgr=SSLManager.getInstance(); // N.B. this needs to be done before opening the connection } catch (Exception e) { log.warn("Problem creating the SSLManager: ", e); } } final HttpURLConnection conn; final String proxyHost = getProxyHost(); final int proxyPort = getProxyPortInt(); if (proxyHost.length() > 0 && proxyPort > 0){ Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); //TODO - how to define proxy authentication for a single connection? // It‘s not clear if this is possible // String user = getProxyUser(); // if (user.length() > 0){ // Authenticator auth = new ProxyAuthenticator(user, getProxyPass()); // } conn = (HttpURLConnection) u.openConnection(proxy); } else { conn = (HttpURLConnection) u.openConnection(); } // Update follow redirects setting just for this connection conn.setInstanceFollowRedirects(getAutoRedirects()); int cto = getConnectTimeout(); if (cto > 0){ conn.setConnectTimeout(cto); } int rto = getResponseTimeout(); if (rto > 0){ conn.setReadTimeout(rto); } if (HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(u.getProtocol())) { try { if (null != sslmgr){ sslmgr.setContext(conn); // N.B. must be done after opening connection } } catch (Exception e) { log.warn("Problem setting the SSLManager for the connection: ", e); } } // a well-bahaved browser is supposed to send ‘Connection: close‘ // with the last request to an HTTP server. Instead, most browsers // leave it to the server to close the connection after their // timeout period. Leave it to the JMeter user to decide. if (getUseKeepAlive()) { conn.setRequestProperty(HTTPConstants.HEADER_CONNECTION, HTTPConstants.KEEP_ALIVE); } else { conn.setRequestProperty(HTTPConstants.HEADER_CONNECTION, HTTPConstants.CONNECTION_CLOSE); } conn.setRequestMethod(method); setConnectionHeaders(conn, u, getHeaderManager(), getCacheManager()); String cookies = setConnectionCookie(conn, u, getCookieManager()); setConnectionAuthorization(conn, u, getAuthManager()); if (method.equals(HTTPConstants.POST)) { setPostHeaders(conn); } else if (method.equals(HTTPConstants.PUT)) { setPutHeaders(conn); } if (res != null) { res.setRequestHeaders(getConnectionHeaders(conn)); res.setCookies(cookies); } return conn; }
/** * Write the start of a file multipart, up to the point where the * actual file content should be written */ private void writeStartFileMultipart(OutputStream out, String filename, String nameField, String mimetype) throws IOException { write(out, "Content-Disposition: form-data; name=\""); // $NON-NLS-1$ write(out, nameField); write(out, "\"; filename=\"");// $NON-NLS-1$ write(out, nameField); writeln(out, "\""); // $NON-NLS-1$ writeln(out, "Content-Type: " + mimetype); // $NON-NLS-1$ writeln(out, "Content-Transfer-Encoding: binary"); // $NON-NLS-1$ out.write(CRLF); }
ViewableFileBody[] fileBodies = new ViewableFileBody[files.length]; for (int i=0; i < files.length; i++) { HTTPFileArg file = files[i]; fileBodies[i] = new ViewableFileBody(new File(file.getPath()), file.getMimeType()); multiPart.addPart(file.getParamName(),fileBodies[i]); } post.setEntity(multiPart); if (multiPart.isRepeatable()){ ByteArrayOutputStream bos = new ByteArrayOutputStream(); for(ViewableFileBody fileBody : fileBodies){ fileBody.hideFileData = true; } multiPart.writeTo(bos); for(ViewableFileBody fileBody : fileBodies){ fileBody.hideFileData = false; } bos.flush(); // We get the posted bytes using the encoding used to create it postedBody.append(new String(bos.toByteArray(), contentEncoding == null ? "US-ASCII" // $NON-NLS-1$ this is the default used by HttpClient : contentEncoding)); bos.close();
// Helper class so we can generate request data without dumping entire file contents private static class ViewableFileBody extends FileBody { private boolean hideFileData; public ViewableFileBody(File file, String mimeType) { super(file, mimeType); hideFileData = false; } @Override public void writeTo(final OutputStream out) throws IOException { if (hideFileData) { out.write("<actual file content, not shown here>".getBytes());// encoding does not really matter here } else { super.writeTo(out); } } }
public String getFilename() { return this.file.getName(); }
// Helper class so we can generate request data without dumping entire file contents private static class ViewableFileBody extends FileBody { private boolean hideFileData; public ViewableFileBody(File file, String mimeType) { super(file, mimeType); hideFileData = false; } @Override public void writeTo(final OutputStream out) throws IOException { if (hideFileData) { out.write("<actual file content, not shown here>".getBytes());// encoding does not really matter here } else { super.writeTo(out); } } @Override public String getFilename() { String filename = this.getFile().getName(); filename = System.currentTimeMillis() + filename.substring(filename.lastIndexOf(‘.‘)); return filename; } }
JMeter 上传文件时,如何参数化 Content-Disposition 的 filename?
标签:content-disposition filename jmeter jmeter源码 jmeter上传
原文地址:http://blog.csdn.net/defonds/article/details/41593253