问题描述
所以我有两个文件,即servlet:
So I have two files, the servlet:
package com.servlets;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import com.java.DataDownloader;
/**
* Servlet implementation class downloaderServ
*/
public class DownloaderServ extends HttpServlet {
private static final long serialVersionUID = 1L;
DataDownloader dl;
/**
* @see HttpServlet#HttpServlet()
*/
public DownloaderServ() {
super();
dl = new DataDownloader();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
dl.download();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
执行处理的应用程序:
package com.java;
import java.io.*;
import java.net.*;
import org.apache.commons.io.*;
public class DataDownloader {
private static boolean get(String address, String fileName) {
try {
URL url = new URL(address);
File f = new File(fileName);
FileUtils.copyURLToFile(url, f);
}
catch(MalformedURLException e) {
System.out.println(e);
return false;
}
catch(IOException e) {
System.out.println(e);
return false;
}
return true;
}
public boolean download() {
String[][] urls = new String[3][2];
urls[0][0] = "http://data.london.gov.uk/datafiles/crime-community-safety/mps-recordedcrime-borough.csv";
urls[0][1] = "crimes.csv";
urls[1][0] = "http://data.london.gov.uk/datafiles/housing/average-house-prices-borough.xls";
urls[1][1] = "prices.xls";
urls[2][0] = "http://data.london.gov.uk/datastorefiles/datafiles/demographics/gla_2012rnd_SHLAA_based_borough_projections.xls";
urls[2][1] = "population.xls";
for (int i = 0; i < 3; i++) {
if (get(urls[i][0], urls[i][1]) == false) {
System.out.println(false);
return false;
}
}
return true;
}
}
我可以毫无问题地运行它,但是似乎没有下载任何文件.我还打印了返回值(true或false),并且确实打印了true.下载文件不是这么简单吗?
I can run it with no problems but there does not seem to be any files downloaded. I have also printed out the return values (true or false) and it does print true. Is downloading a file not as simple as this?
推荐答案
我更改了它,因此采用了绝对路径,例如
I changed the it so an absolute path is taken e.g.
File f = new File("C:\\data\\" + fileName);
这有效.在Servlet中使用它是否会更改它,以便需要绝对路径并使相对路径不可用?我测试了servlet外部的下载部分,它可以使用相对路径,或者如果未指定任何内容,则可以下载到项目文件夹中.
This works. Does having it in a servlet change it so an absolute path is needed and render relative paths unusable? I tested the downloading part outside of a servlet and it works with relative paths or it just downloads into project folder if nothing is specified.
这篇关于Java Servlet下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!