当前位置: 代码迷 >> Java Web开发 >> 请问有关文件上传的有关问题。有会的进来帮帮忙哦、
  详细解决方案

请问有关文件上传的有关问题。有会的进来帮帮忙哦、

热度:5229   发布时间:2013-02-25 21:18:05.0
请教有关文件上传的问题。。。。有会的进来帮帮忙哦、、、、、、、、
在机房内,我们相邻的两台电脑。。我下载往另一台机器上东西,谁知道怎么实现啊??
也就是我想在网页中实现文件下载。。。从客户端下载服务器上的东西


客户端连接
<a href="MyDownloadAction.action">点击下载</a>
Action
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class MyDownloadAction extends ActionSupport {


/**

*/
private static final long serialVersionUID = 1L;

public InputStream getDownloadFile(){

InputStream is=ServletActionContext.getServletContext().getResourceAsStream("http://10.3.5.32:8080/ROOT/1.txt");
System.out.println(is);
return is;
}

@Override
public String execute() throws Exception {

return SUCCESS;
}

}

上面的方法下载本机能实现
可是下载另一台机器上的资源就实现不了了
高手帮我看看把、、、、

------解决方案--------------------------------------------------------
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.org.hydata.action;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.org.hydata.dao.FileDao;

/**
 * MyEclipse Struts Creation date: 09-11-2008
 * 
 * XDoclet definition:
 * 
 * @struts.action validate="true"
 */
public class DownAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute

* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws UnsupportedEncodingException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException {

//得到下载文件的名字
String filename=request.getParameter("filename");
// System.out.print("filename11=="+filename);
//解决中文乱码问题
filename=request.getContextPath()+"/upload1"+"/"+filename;
// System.out.print(filename);
filename=new String(filename.getBytes("iso-8859-1"),"gbk");
//创建file对象

filename=request.getRealPath(filename);
//System.out.print("filename12=="+filename);
File file=new File(filename);
//设置response的编码方式
response.setContentType("application/x-msdownload");
//写明要下载的文件的大小
response.setContentLength((int)file.length());
//设置附加文件名
response.setHeader("Content-Disposition","attachment;filename="+filename);
//解决中文乱码
//response.setHeader("Content-Disposition","attachment;filename="+new String
//(filename.getBytes("gbk"),"iso-8859-1"));
//读出文件到i/o流
FileInputStream fis;
try {
fis = new FileInputStream(file);
BufferedInputStream buff=new BufferedInputStream(fis);
byte [] b=new byte[1024];//相当于我们的缓存
  相关解决方案