已经设置了头文件信息为attachment;filename=,为啥下载的时候还是后缀名.action

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了已经设置了头文件信息为attachment;filename=,为啥下载的时候还是后缀名.action相关的知识,希望对你有一定的参考价值。

我的文件已经设置了头信息,为什么还是下载的时候会是后缀名 .action这样的。
给大神看下。
首先是在被调用方法downFile() 中写了:
final String ct = "video/mp4";
String fileName = "a.mp4";
然后调用了方法:
FileUtil.wireteFile(response, is, fileName, ct);
下面是 这个 wireteFile方法中的内容,就是不明白为什么我设置了文件名
头信息,还是保存出来是.action这样的。

int lio = fileName.lastIndexOf("/");
if (lio != -1)
fileName = fileName.substring(fileName.lastIndexOf("/"));
fileName = fileName.substring(1, fileName.length());

// response.setContentLength(is.available());
response.addHeader(

"content-disposition"

, "attachment;filename="

+ java.net.URLEncoder.encode(fileName, Charset.UTF_8));
if (EmptyUtil.isEmpty(contentType, true))
contentType =

"application/octet-stream"

;


response.setContentType(contentType);
os = response.getOutputStream();
byte[] b = new byte[1024];
int size = is.read(b);
while (size > 0)
os.write(b, 0, size);
size = is.read(b);

你的配置文件对下载进行了拦截,首先你要看下你的配置文件。如果是Springmvc里面要设置的。 参考技术A attachment;filename=改成 attachment; filename=

分号后面加个空格
参考技术B 不要用下载工具下. 用浏览器.追问

。。。 用苹果的手机默认浏览器,无法打开,现在项目要求。

php 将文件移动到uploads director,在MG中插入Attachment,为Post设置特色图像

// MOVE FILE INTO THE WP UPLOADS DIRECTORY AND THEN "UPLOAD" INTO
// AS AN ATTACHMENT IN THE MEDIA GALLERY< AND ASSOCIATED IT TO A SPECIFIC POST

//Start image url 
	$startImgUrl =  __DIR__ . $file['tmp_name'];
	//Just the file name (with extension)
	$filename = basename($startImgUrl);

	//To Uploads directory Path/Url 
	$wordpress_upload_dir = wp_upload_dir()['path'];

	//fetch the image to put into Media Gallery
	$contents = file_get_contents($startImgUrl);

	// Testing success
	if(file_put_contents($wordpress_upload_dir . "/" . $filename, $contents)) {

        // UPLOAD ATTACHMENT
		// $filename should be the path to a file in the upload directory.
		$filename = $wordpress_upload_dir . "/" . $filename;

		// Check the type of file. We'll use this as the 'post_mime_type'.
		$filetype = wp_check_filetype( basename( $filename ), null );
		$ext = $filetype['ext'];
		$mime = $filetype['type'];

		// Get the path to the upload directory.
		$wp_upload_dir = wp_upload_dir();

		// Prepare an array of post data for the attachment.
		$attachment = array(
			'guid'           => $newFile, //full path
			'post_mime_type' => $mime,
			'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
			'post_content'   => '',
			'post_status'    => 'inherit'
		);

		// Insert the attachment.
		$attach_id = wp_insert_attachment( $attachment, $filename);

        // SET FEATURED IMAGE OF POST

        if(isset($attach_id) && ($attach_id > 0)):
            // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
            if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
                require_once( ABSPATH . 'wp-admin/includes/image.php' );
            }

            // The ID of the post this attachment is for. ()
            $post_id = $_POST["post_id"];
            
            // Generate the metadata for the attachment, and update the database record.
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
            wp_update_attachment_metadata( $attach_id, $attach_data );

            set_post_thumbnail( $post_id, $attach_id );
        endif;


	} else {

		//echo "file wasn't moved";
  }

以上是关于已经设置了头文件信息为attachment;filename=,为啥下载的时候还是后缀名.action的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中设置电子邮件附件名称

php 将文件移动到uploads director,在MG中插入Attachment,为Post设置特色图像

把ucos移植到stm32后写的一个简单程序。编译时出错。已经包含了头文件stm32f10x_lib.h

如果 java 以“content-disposition”、“attachment;filename=FileName.xls”的形式发送响应,如何通过 ajax 在现有窗口中下载文件

上传伪技术~很多人都以为判断了后缀,判断了ContentType,判断了头文件就真的安全了。是吗?

3构建bass服务及model