java文件保存位置_java – 将上传的文件保存在特定位置

我有以下代码处理服务器上的文件上传.但是如何将文件保存到服务器上的特定位置

import gwtupload.server.UploadAction;

import gwtupload.server.exceptions.UploadActionException;

import org.apache.commons.fileupload.FileItem;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Hashtable;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* This is an example of how to use UploadAction class.

*

* This servlet saves all received files in a temporary folder,

* and deletes them when the user sends a remove request.

*

* @author Manolo Carrasco Moñino

*

*/

public class SampleUploadServlet extends UploadAction {

private static final long serialVersionUID = 1L;

Hashtable receivedContentTypes = new Hashtable();

/**

* Maintain a list with received files and their content types.

*/

Hashtable receivedFiles = new Hashtable();

/**

* Override executeAction to save the received files in a custom place

* and delete this items from session.

*/

@Override

public String executeAction(HttpServletRequest request, List sessionFiles) throws UploadActionException {

String response = "";

int cont = 0;

for (FileItem item : sessionFiles) {

if (false == item.isFormField()) {

cont++;

try {

/// Create a new file based on the remote file name in the client

// String saveName = item.getName().replaceAll("[\\\\/>

// File file =new File("/tmp/" + saveName);

/// Create a temporary file placed in /tmp (only works in unix)

// File file = File.createTempFile("upload-", ".bin", new File("/tmp"));

/// Create a temporary file placed in the default system temp folder

File file = File.createTempFile("upload-", ".bin");

item.write(file);

/// Save a list with the received files

receivedFiles.put(item.getFieldName(), file);

receivedContentTypes.put(item.getFieldName(), item.getContentType());

/// Compose a xml message with the full file information

response += "" + item.getFieldName() + "\n";

response += "" + item.getName() + "\n";

response += "" + item.getSize() + "\n";

response += "" + item.getContentType() + "\n";

}

catch (Exception e)

{

}

}

}

/// Remove files from session because we have a copy of them

removeSessionFileItems(request);

/// Send information of the received files to the client.

return "\n" + response + "\n";

}

/**

* Get the content of an uploaded file.

*/

@Override

public void getUploadedFile(HttpServletRequest request, HttpServletResponse response) throws IOException {

String fieldName = request.getParameter(PARAM_SHOW);

File f = receivedFiles.get(fieldName);

if (f != null) {

response.setContentType(receivedContentTypes.get(fieldName));

FileInputStream is = new FileInputStream(f);

copyFromInputStreamToOutputStream(is, response.getOutputStream());

} else {

renderXmlResponse(request, response, ERROR_ITEM_NOT_FOUND);

}

}

/**

* Remove a file when the user sends a delete request.

*/

@Override

public void removeItem(HttpServletRequest request, String fieldName) throws UploadActionException {

File file = receivedFiles.get(fieldName);

receivedFiles.remove(fieldName);

receivedContentTypes.remove(fieldName);

if (file != null) {

file.delete();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值