我有一个java servlet,它接受用户上传到我的web应用程序的图像.
我有另一台服务器(运行php),它将托管所有图像.如何从我的jsp服务器获取图像到我的php服务器?流程将是这样的:
public class ServletImgUpload extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// get image user submitted
// try sending it to my php server now
// return success or failure message back to user
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
好吧,所以我有这些代码,当我提出请求时,我想包含一些HTTP标头信息.我该怎么做呢?
public boolean call(String apiCall) {
if (this.apiCalls.containsKey(apiCall)) {
try{
URL url = this.apiCalls.get(apiCall);
url = new URL(url.toString() + "?memberid=76710");
URLConnection urlConn = url.openConnection();
InputStream is = urlConn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
this.responseResultText = new String(baf.toByteArray());
return true;
} catch(Exception e){
this.responseResultText = e.getMessage();
return false;
}
}
this.responseResultText = "API call " + apiCall + " doesn't exist.";
return false;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我已经为我的Android设备编写了一些代码来登录网站,https并从结果页面中解析出一些数据.一个HttpGet发生抢先拿到需要登陆一些信息,那么HttpPost做实际的登录过程.
:下面的代码在Eclipse中的Java项目,具有构建路径上的下列JAR文件的伟大工程httpcore-4.1-beta2.jar,httpclient-4.1-alpha2.jar,httpmime-4.1-alpha2.jar,commons-logging-1.1.1.jar.
public static MyBean gatherData(String username, String password) {
MyBean myBean = new MyBean();
try {
HttpResponse response = doHttpGet(URL_PAGE_LOGIN, null, null);
System.out.println("Got login page");
String content = EntityUtils.toString(response.getEntity());
String token = ContentParser.getToken(content);
String cookie = getCookie(response);
System.out.println("Performing login");
System.out.println("token = "+token +" || cookie = "+cookie);
response = doLoginPost(username,password,cookie, token);
int respCode = response.getStatusLine().getStatusCode();
if (respCode != 302) {
System.out.println("ERROR: not a 302 redirect!: …Run Code Online (Sandbox Code Playgroud) 我想从Java应用程序调用Servlet.问题是,调用似乎没有到达Servlet.我没有得到任何错误,但没有到达Servlet中的第一个输出"doPost".如果我在网络浏览器中打开URL,我当然得到了GET不支持的错误等,但至少我看到,有些事情发生了.
我使用以下代码(ActionPackage类只包含参数Vector并且是Serializable):
Java应用程序:
ActionPackage p = new ActionPackage();
p.addParameter("TEST", "VALUE");
System.out.println(p);
URL gwtServlet = null;
try {
gwtServlet = new URL("http://localhost:8888/app/PushServlet");
HttpURLConnection servletConnection = (HttpURLConnection) gwtServlet.openConnection();
servletConnection.setRequestMethod("POST");
servletConnection.setDoOutput(true);
ObjectOutputStream objOut = new ObjectOutputStream(servletConnection.getOutputStream());
objOut.writeObject(p);
objOut.flush();
objOut.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
Servlet的:
public class PushServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
ObjectInputStream objIn …Run Code Online (Sandbox Code Playgroud) 我正在编写一个Java桌面客户端,它将使用post请求通过线路将多个文件发送到servlet.在servlet中,我从请求中获取输入流以接收文件.当从流中读取文件时,servlet将逐个文件写入磁盘.
该实现有几个要求:
我曾考虑在流中插入标记,因此我知道一个文件何时结束而下一个文件何时开始.然后我编写一些代码来解析servlet中的流,并开始编写适当的下一个文件.
这是事情......当然有一个图书馆可以做到这一点.我看过apache的公地,一无所获.Commons File Upload很有意思,但由于上传来自Java应用程序,而不是浏览器,它只能解决接收端,而不是发送.
对库的任何想法都可以轻松地允许跨单个流进行多个文件传输,即使对于非常大的文件也具有固定的内存期望?
谢谢.
我正在尝试使用JDK java.net.URI构建URI 。
我想附加一个绝对URI对象,一个查询(在String中)。例如:
URI base = new URI("http://example.com/something/more/long");
String queryString = "query=http://local:282/rand&action=aaaa";
URI query = new URI(null, null, null, queryString, null);
URI result = base.resolve(query);
Run Code Online (Sandbox Code Playgroud)
理论(或我的想法)是决心应该返回:
http://example.com/something/more/long?query=http://local:282/rand&action=aaaa
Run Code Online (Sandbox Code Playgroud)
但是我得到的是:
http://example.com/something/more/?query=http://local:282/rand&action=aaaa
Run Code Online (Sandbox Code Playgroud)
为什么#resolve() “吃掉”最后一条路?如果新的URI(query)构建为:
URI query = new URI(null, null, base.getPath(), queryString, null);
Run Code Online (Sandbox Code Playgroud)
它运作良好。
我有一个看起来像这样的 HTML 表单:
<form name="form1" method="post" action="/confirm.asp">
<input type="text" name="data1" size="20" value=""><br>
<input type="text" name="data2" size="20" value=""><br>
<input type="Submit" name=submit value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)
我想用Java来传递数据data1和data2和读取表单提交时下面的页面。由于这是一个 method=post,我不能使用http://somesite.com/confirm.asp?data1=foo&data2=foo.
可以帮忙吗?
我有非常简单的JSF bean,如下所示:
import org.jboss.seam.annotations.Name;
@Name(Sample.NAME)
public class Sample {
public static final String NAME="df";
private String text = "text-test";
public void sampleM(){
System.out.println("Test: "+text);
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Run Code Online (Sandbox Code Playgroud)
和JSF表单连接这个组件:
<h:form id="sampleForm">
<h:commandButton id="sampleButton" action="#{df.sampleM()}" value="ok" />
</h:form>
Run Code Online (Sandbox Code Playgroud)
现在,我想以编程方式将POST请求发送到此表单.
根据我的调查,这里的关键是POST参数.正确选择可以得到正确的结果(字符串'测试:文本测试'打印在serwer的控制台上).
所以问题是:我应该如何选择正确的POST数据?
上面显示的JSF表单生成此HTML表单:
<form id="sampleForm" name="sampleForm" method="post" action="/pages/main/main.smnet" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="sampleForm" value="sampleForm" />
<input id="sampleForm:sampleButton" type="submit" name="sampleForm:sampleButton" value="ok" />
<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="j_id65" autocomplete="off" /> …Run Code Online (Sandbox Code Playgroud) 我正在尝试从此目录下载所有文件.但是,我只能将它作为一个文件下载.我能做什么?我试图搜索这个问题,这让人感到困惑,人们开始建议使用httpclients.感谢您的帮助,这是我的代码到目前为止.有人建议我使用输入流来获取目录中的所有文件.那么那会进入阵列吗?我在http://docs.oracle.com/javase/tutorial/networking/urls/尝试了这个教程,但它没有帮助我理解.
//ProgressBar/Install
String URL_LOCATION = "http://www.futureretrogaming.tk/gamefiles/ProfessorPhys/";
String LOCAL_FILE = filelocation.getText() + "\\ProfessorPhys\\";
try {
java.net.URL url = new URL(URL_LOCATION);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("User-Agent", "Mozilla/4.76");
//URLConnection connection = url.openConnection();
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
int available = stream.available();
byte b[]= new byte[available];
stream.read(b);
File file = new File(LOCAL_FILE);
OutputStream out = new FileOutputStream(file);
out.write(b);
} catch (Exception e) {
System.err.println(e);
}
Run Code Online (Sandbox Code Playgroud)
我还发现这个代码将返回要下载的文件列表.有人可以帮我合并这两个代码吗?
public class GetAllFilesInDirectory {
public static void main(String[] args) throws IOException {
File dir = …Run Code Online (Sandbox Code Playgroud) 我正在测试一个代码示例,但它总是出错 connection.setDoInput(true);
HttpsURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String urlServer = "https://www.myurl.com/upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead = 0;
int bytesAvailable = 0;
int bufferSize = 0;
byte[] buffer = null;
int maxBufferSize = 1*1024*1024;
try {
FileInputStream fileInputStream = new FileInputStream(new File(params[0]));
URL url = new URL(urlServer);
connection = (HttpsURLConnection) url.openConnection();
connection.setConnectTimeout(1000);
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method …Run Code Online (Sandbox Code Playgroud)