请查看以下代码
FTPDownload.java
package ftp;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
/**
* A program demonstrates how to upload files from local computer to a remote
* FTP server using Apache Commons Net API.
* @author www.codejava.net
*/
public class FTPDownload {
public void download() {
String server = "ftp.***.com";
int port = 21;
String user = "****";
String pass = "****";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// APPROACH #1: using retrieveFile(String, OutputStream)
String remoteFile1 = "/doctor/Katiya Karun.mp3";
File downloadFile1 = new File("D:/Katiya Karun.mp3");
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
FTPDownloadServlet.java
package ftp;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FTPDownloadServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("Downloading");
FTPDownload f = new FTPDownload();
f.download();
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
的index.jsp
<form action="FTPDownloadServlet" method="post">
<input type="submit" value="Download" />
</form>
上面的代码使用的是Apache Commons Net
库。使用上面的代码,您可以从FTP服务器下载文件。
但是,此程序有一个“硬编码”位置来下载文件。相反,我需要在单击提交按钮时打开“另存为”对话框。更好的是,我不想下载,而是选择在网页的新标签页中打开文件,以便人们可以只在他们想要的时候点击并下载(我的真实文件只是图片和pdf)。
我该怎么做?
答案 0 :(得分:-1)
客户端无论如何都要下载文件。你可以使用InputStream。
public class AllUsersListAdapter implements ListAdapter, Filterable {
public List<HEADER> allUsers;
public List<HEADER>filteredUsers;
Context context;
int layoutResourceId;
private Filter mFilter;
private DataSetObservable mDataSetObservable = new DataSetObservable();
public AllUsersListAdapter(Context context, int resource, List<HEADER> allUsers) {
this.filteredUsers = this.allUsers = allUsers;
this.context = context;
layoutResourceId = resource;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
mDataSetObservable.registerObserver(observer);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
mDataSetObservable.unregisterObserver(observer);
}
@Override
public int getCount() {
return filteredUsers.size();
}
@Override
public HEADER getItem(int position) {
return filteredUsers.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.userName = (TextView) row.findViewById(R.id.userName);
holder.desc = (TextView) row.findViewById(R.id.userDesc);
holder.rowCheckBox = (CheckBox) row.findViewById(R.id.rowCheckBox);
holder.rowCheckBox.setVisibility(View.VISIBLE);
HEADER header = getItem(position);
if(CreateGroupHelperClass.selectedUsers.contains(header))
{
holder.rowCheckBox.setChecked(true);
}
final ViewHolder finalHolder = holder;
holder.rowCheckBox.setTag(allUsers.get(position));
holder.rowCheckBox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
HEADER element = (HEADER) finalHolder.rowCheckBox
.getTag();
if (isChecked) {
CreateGroupHelperClass.unSelectedUsers.remove(element);
CreateGroupHelperClass.selectedUsers.add(element);
}
else {
CreateGroupHelperClass.selectedUsers.remove(element);
CreateGroupHelperClass.unSelectedUsers.add(element);
}
}
});;
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
HEADER header = getItem(position);
holder.userName.setText(header.getNAME());
holder.desc.setText(header.getFRONTEND_ID());
return row;
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
@Override
public boolean isEnabled(int position) {
return true;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ContactFilter();
}
return mFilter;
}
private class ContactFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint == null || constraint.length() == 0) {
filterResults.values = allUsers;
filterResults.count = allUsers.size();
} else {
final String lastToken = constraint.toString().toLowerCase();
final int count = allUsers.size();
final List<HEADER> list = new ArrayList<HEADER>();
HEADER header;
for (int i = 0; i < count; i++) {
header = allUsers.get(i);
if (header.getNAME().toLowerCase().startsWith(lastToken)) {
list.add(header);
}
}
filterResults.values = list;
filterResults.count = list.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredUsers = (List<HEADER>)results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
public void clearSelectedUsersList() {
if (allUsers != null) allUsers.clear();
}
public void notifyDataSetChanged() {
mDataSetObservable.notifyChanged();
}
public void notifyDataSetInvalidated() {
mDataSetObservable.notifyInvalidated();
}
static class ViewHolder {
TextView userName;
TextView desc;
ImageButton cancel_image_button;
CheckBox rowCheckBox;
}
}
然后通常在客户端显示它,并管理保存操作。
如果你使用摇摆this example可能会有所帮助。
保存方法:
InputStream remoteFileStream = retrieveFileStream(remoteFile1)